Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
ci: add bin/ci to run the affected CI checks locally before push (CUA-3134) (#6383)
cua-agents[bot]5c94c7bc67eb
diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.mdindex 761e45ec2d..42aa88390d 100644--- a/.github/CONTRIBUTING.md+++ b/.github/CONTRIBUTING.md@@ -80,6 +80,8 @@ GitHub issue form. Longer proposals and diagrams remain in this repository under2. Keep changes scoped to the component that owns the behavior.3. Add or update tests that observe the public effect of the change.4. Run the applicable commands in [`TESTING.md`](../docs/contributing/TESTING.md).+ Before you push, run `bin/ci`: it runs the CI checks your change affects and+ prints the fix for each failure (see [`CI.md`](../docs/contributing/CI.md#run-the-checks-locally-first-binci)).5. Run the formatters and linters owned by the changed component.6. Open a focused pull request that explains behavior, validation, and known gaps.diff --git a/.github/workflows/ci-test-scripts.yml b/.github/workflows/ci-test-scripts.ymlindex ccc5f3fef1..a3cadeda4d 100644--- a/.github/workflows/ci-test-scripts.yml+++ b/.github/workflows/ci-test-scripts.yml@@ -58,6 +58,7 @@ on:- "libs/cua-driver/rust/crates/cua-driver-e2e/tests/**"- "scripts/install/install.ps1"- "scripts/ci/**"+ - "bin/ci"- "scripts/ci/macos/**"- "libs/cua-driver/tests/perception-demo/**"- "libs/cua-s1/ci/**"@@ -162,6 +163,13 @@ jobs:cd .github/scriptspytest tests/ -v+ # bin/ci mirrors workflow commands; this fails when a workflow stops+ # running one of them, so local and hosted checks cannot drift apart.+ - name: Test bin/ci and its sync with the workflows+ run: |+ python3 -m unittest discover -s scripts/ci/tests -v+ python3 bin/ci --verify-sync+- name: Test Cua Driver install and uninstall scriptsrun: python -m pytest libs/cua-driver/scripts/tests/ -vdiff --git a/AGENTS.md b/AGENTS.mdindex 8b71729e3c..a2893c06b2 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -65,6 +65,15 @@ that are easy to miss:- Tests that start VMs, containers, or host input are opt-in(off by default) and clean up what they create.+## Before you push: `bin/ci`++Run `bin/ci` before every push, and `bin/ci --fix` when it reports a failure+it can repair (formatting, lockfiles, generated bindings and reference docs).+It runs the CI checks your diff against `origin/main` affects, in a few+minutes, and prints the exact fix command for each failure. A green `bin/ci`+does not replace CI: the end-to-end, image and native Windows lanes still run+there. See [`CI.md`](docs/contributing/CI.md#run-the-checks-locally-first-binci).+## Cross-platform Cua Driver behaviorTreat user-visible Cua Driver behavior as a cross-platform contract. Implementdiff --git a/bin/ci b/bin/cinew file mode 100755index 0000000000..81f4540bd8--- /dev/null+++ b/bin/ci@@ -0,0 +1,1190 @@+#!/usr/bin/env python3+"""bin/ci: run, on your machine, the CI checks that your change affects.++It reads the files that differ from the merge base with origin/main (plus+anything uncommitted), picks the checks those files can break, runs them in+parallel and prints the exact command that fixes each failure. The commands+are the ones the GitHub workflows run; `bin/ci --verify-sync` fails when a+workflow stops running one of them, so the two cannot drift apart unseen.++ bin/ci checks for the files you changed (default)+ bin/ci --quick seconds: formatting, lockfiles, headers, lint; no builds+ bin/ci --full every check, whatever you changed+ bin/ci --fix apply the safe fixers (formatters, lockfiles,+ generators), then check again+ bin/ci --list show the plan without running it+ bin/ci --only ID,... run only these checks (ids from --list)+ bin/ci --signoff after a green run, set the `local-ci` commit status+ bin/ci --install-hook run bin/ci before every git push (opt-in)++Run `bin/ci --help` for every option. Docs: docs/contributing/CI.md.+"""++from __future__ import annotations++import argparse+import concurrent.futures+import json+import os+import re+import shlex+import shutil+import subprocess+import sys+import tempfile+import threading+import time+from dataclasses import dataclass, field+from pathlib import Path+from typing import Callable, Dict, Iterable, List, Optional, Sequence, Tuple++# CUA_CI_ROOT runs this copy against another checkout (used to replay old commits).+ROOT = Path(os.environ.get("CUA_CI_ROOT") or Path(__file__).resolve().parent.parent).resolve()+HOOK_MARKER = "# bin/ci pre-push hook"+CHAINED_HOOK = "pre-push.pre-bin-ci"+STATUS_CONTEXT = "local-ci"++# ---------------------------------------------------------------- model+++@dataclass+class Check:+ id: str+ title: str+ cmd: str # bash, run from `cwd`+ cwd: str = "."+ fix: Optional[str] = None # shell command that repairs a failure+ safe_fix: bool = False # bin/ci --fix may run `fix` on its own+ fix_from_output: Optional[str] = None # regex: group 1 is a fix command+ hint: str = "" # shown on failure when there is no fix command+ tools: Tuple[str, ...] = () # executables that must exist+ requires: Tuple[Tuple[str, str], ...] = () # (path that must exist, how to create it)+ group: Optional[str] = None # checks in one group run one at a time+ heavy: bool = False # builds something large; skipped by --quick+ tier: str = "quick" # quick | build (skipped by --quick)+ env: Dict[str, str] = field(default_factory=dict)+ ci: Tuple[Tuple[str, str], ...] = () # (workflow, text it must contain)+ advisory: bool = False # never fails the run+ isolate_home: bool = False # run with an empty HOME, as on a fresh CI runner+ classify: Optional[Callable[[str, int], Tuple[str, str]]] = None # (log, rc) -> (status, detail)+ skip_reason: Optional[str] = None # planned but cannot run here+++@dataclass+class Result:+ check: Check+ status: str # pass | FAIL | skip | fixed | warn+ seconds: float = 0.0+ log: Optional[Path] = None+ detail: str = ""+++# ---------------------------------------------------------------- shell helpers+++def sh(cmd: Sequence[str], cwd: Optional[Path] = None, check: bool = False) -> str:+ out = subprocess.run(list(cmd), cwd=str(cwd or ROOT), capture_output=True, text=True)+ if check and out.returncode != 0:+ raise RuntimeError(f"{' '.join(cmd)}: {out.stderr.strip()}")+ return out.stdout+++def git(*args: str) -> str:+ return sh(["git", *args])+++def have(tool: str) -> bool:+ return shutil.which(tool) is not None+++def color(text: str, code: str) -> str:+ if not sys.stdout.isatty() or os.environ.get("NO_COLOR"):+ return text+ return f"\033[{code}m{text}\033[0m"+++# ---------------------------------------------------------------- toolchains+++def pinned_rust_channel() -> Optional[str]:+ """The toolchain CI pins (dtolnay/[email protected] and rust-toolchain.toml)."""+ for p in (ROOT / "libs/cua/rust-toolchain.toml", ROOT / "libs/cua-driver/rust/rust-toolchain.toml"):+ if p.exists():+ m = re.search(r'^channel\s*=\s*"([^"]+)"', p.read_text(), re.M)+ if m:+ return m.group(1)+ return None+++def rust_env(notes: List[str]) -> Dict[str, str]:+ """PATH and RUSTUP_TOOLCHAIN so `cargo` is the pinned rustup toolchain.++ A Homebrew `cargo` ahead of the rustup proxies ignores rust-toolchain.toml,+ and a newer rustfmt or clippy reports different findings than CI does.+ """+ env: Dict[str, str] = {}+ channel = pinned_rust_channel()+ rustup = shutil.which("rustup")+ if not rustup or not channel:+ if have("cargo"):+ notes.append("rustup not found: using the cargo on PATH; rustfmt and clippy may differ from CI")+ return env+ installed = sh([rustup, "toolchain", "list"])+ if not any(line.startswith(channel) for line in installed.splitlines()):+ notes.append(+ f"Rust {channel} (the CI toolchain) is not installed; using the default toolchain. "+ f"Install it: rustup toolchain install {channel} --component clippy,rustfmt"+ )+ return env+ env["RUSTUP_TOOLCHAIN"] = channel+ proxy_dirs = [Path(rustup).resolve().parent, Path(rustup).parent]+ brew = Path(rustup).resolve().parent.parent / "bin" # Homebrew keeps the proxies next to rustup+ for d in (Path("/opt/homebrew/opt/rustup/bin"), Path("/usr/local/opt/rustup/bin"), Path.home() / ".cargo/bin", brew, *proxy_dirs):+ if (d / "cargo").exists() and (d / "cargo-clippy").exists():+ current = shutil.which("cargo")+ if current is None or Path(current).parent != d:+ env["PATH"] = f"{d}{os.pathsep}{os.environ.get('PATH', '')}"+ break+ return env+++def pnpm_cmd(version: Optional[str]) -> str:+ """pnpm at the version a package pins (CI's pnpm/action-setup reads packageManager)."""+ if not version:+ return "pnpm"+ # Ask from outside the repo: pnpm reports the root packageManager version+ # inside it, whatever version actually runs.+ local = sh(["pnpm", "--version"], cwd=Path(tempfile.gettempdir())).strip() if have("pnpm") else ""+ if local.split(".")[0] == version.split(".")[0]:+ return "pnpm"+ return f"npx -y pnpm@{version}"+++def package_manager(pkg_dir: Path) -> Optional[str]:+ p = pkg_dir / "package.json"+ if not p.exists():+ return None+ m = re.match(r"pnpm@([0-9.]+)", json.loads(p.read_text()).get("packageManager", ""))+ return m.group(1) if m else None+++# ---------------------------------------------------------------- change set+++@dataclass+class Changes:+ base: str+ base_desc: str+ files: List[str] # every changed path, deleted ones included+ full: bool = False++ def touches(self, pattern: str) -> List[str]:+ if self.full:+ return ["(full run)"]+ rx = re.compile(pattern)+ return [f for f in self.files if rx.search(f)]++ def existing(self, pattern: str, suffixes: Iterable[str] = ()) -> List[str]:+ rx = re.compile(pattern)+ suf = tuple(suffixes)+ return [f for f in self.files if rx.search(f) and (ROOT / f).is_file() and (not suf or f.endswith(suf))]+++def find_base(explicit: Optional[str]) -> Tuple[str, str]:+ if explicit:+ sha = git("rev-parse", "--verify", f"{explicit}^{{commit}}").strip()+ if not sha:+ sys.exit(f"bin/ci: cannot resolve --base {explicit}")+ return sha, explicit+ for ref in ("origin/main", "main"):+ if git("rev-parse", "-q", "--verify", ref).strip():+ mb = git("merge-base", "HEAD", ref).strip()+ if mb:+ return mb, f"merge base with {ref}"+ sys.exit("bin/ci: cannot find origin/main; run `git fetch origin main` or pass --base REF")+++def changed_files(base: str) -> List[str]:+ out = set()+ for args in (("diff", "--name-only", f"{base}...HEAD"), ("diff", "--name-only", "HEAD"), ("ls-files", "--others", "--exclude-standard")):+ out.update(line for line in git(*args).splitlines() if line)+ return sorted(out)+++# ---------------------------------------------------------------- Rust workspaces++# The Windows CI lane of each workspace, read from its workflow so the+# cross-compiled local run checks exactly the packages CI does:+# workspace -> (workflow, regex whose group 1 is the cargo arguments, local cargo subcommand)+WINDOWS_LANES = {+ "libs/cua": ("ci-cua-sdk.yml", r"- name: Clippy \(native\)\s+run: >\s+cargo clippy (--locked .*? -- -D warnings)", "clippy"),+ "libs/cua-spacesd": ("ci-cua-spacesd.yml", r"- name: Clippy\s+run: cargo clippy (--workspace --all-targets --locked -- -D warnings)", "clippy"),+ "libs/cua-driver/rust": ("ci-rust-windows.yml", r"run: cargo test (-p cua-driver .*?--all-targets) --no-run --locked", "check"),+}+++def windows_lane(root: str) -> Optional[Tuple[str, List[str]]]:+ """(cargo args, packages) of the workspace's Windows CI lane, or None."""+ lane = WINDOWS_LANES.get(root)+ if not lane:+ return None+ path = ROOT / ".github/workflows" / lane[0]+ m = re.search(lane[1], path.read_text(), re.S) if path.exists() else None+ if not m:+ return None+ args = " ".join(m.group(1).split())+ return args, re.findall(r"-p (\S+)", args)+++@dataclass+class Workspace:+ root: str # repo-relative dir+ members: Dict[str, str] # package name -> repo-relative dir+ path_deps: Dict[str, List[str]] # package -> in-workspace packages it depends on+++_ws_cache: Dict[str, Optional[Workspace]] = {}+_ws_lock = threading.Lock()+++def package_of(path: str) -> Optional[Tuple[str, str]]:+ """(package dir, package name) of the crate that owns `path`."""+ d = (ROOT / path).parent+ while d != ROOT and ROOT in d.parents:+ manifest = d / "Cargo.toml"+ if manifest.exists():+ text = manifest.read_text(errors="replace")+ m = re.search(r'^\[package\][^\[]*?^name\s*=\s*"([^"]+)"', text, re.M | re.S)+ if m:+ return str(d.relative_to(ROOT)), m.group(1)+ d = d.parent+ return None+++def workspace_of(pkg_dir: str, env: Dict[str, str]) -> Optional[Workspace]:+ out = subprocess.run(+ ["cargo", "locate-project", "--workspace", "--message-format", "plain"],+ cwd=str(ROOT / pkg_dir), capture_output=True, text=True, env={**os.environ, **env},+ )+ if out.returncode != 0:+ return None+ root = str(Path(out.stdout.strip()).parent.relative_to(ROOT))+ with _ws_lock:+ if root in _ws_cache:+ return _ws_cache[root]+ meta = subprocess.run(+ ["cargo", "metadata", "--no-deps", "--format-version", "1", "--offline"],+ cwd=str(ROOT / root), capture_output=True, text=True, env={**os.environ, **env},+ )+ ws: Optional[Workspace] = None+ if meta.returncode == 0:+ data = json.loads(meta.stdout)+ members = {p["name"]: str(Path(p["manifest_path"]).parent.relative_to(ROOT)) for p in data["packages"]}+ deps = {p["name"]: sorted({d["name"] for d in p["dependencies"] if d.get("path") and d["name"] in members}) for p in data["packages"]}+ ws = Workspace(root, members, deps)+ with _ws_lock:+ _ws_cache[root] = ws+ return ws+++def dependents(ws: Workspace, changed: Iterable[str]) -> List[str]:+ rev: Dict[str, List[str]] = {}+ for pkg, deps in ws.path_deps.items():+ for d in deps:+ rev.setdefault(d, []).append(pkg)+ seen, todo = set(changed), list(changed)+ while todo:+ for r in rev.get(todo.pop(), []):+ if r not in seen:+ seen.add(r)+ todo.append(r)+ return sorted(seen - set(changed))+++def deps_closure(ws: Workspace, roots: Iterable[str]) -> set:+ seen, todo = set(), [r for r in roots if r in ws.members]+ while todo:+ p = todo.pop()+ if p not in seen:+ seen.add(p)+ todo.extend(ws.path_deps.get(p, []))+ return seen+++def changed_crates(changes: Changes, env: Dict[str, str]) -> Dict[str, Tuple[Workspace, set]]:+ """workspace root -> (workspace, changed package names)."""+ out: Dict[str, Tuple[Workspace, set]] = {}+ for f in changes.files:+ if not (f.endswith(".rs") or f.endswith("Cargo.toml") or "/src/" in f or "/tests/" in f or "/benches/" in f):+ continue+ owner = package_of(f)+ if not owner:+ continue+ pkg_dir, name = owner+ ws = workspace_of(pkg_dir, env)+ if not ws or name not in ws.members:+ continue+ out.setdefault(ws.root, (ws, set()))[1].add(name)+ return out+++# ---------------------------------------------------------------- the checks++LOCKFILE_RX = r"(^|/)(Cargo\.(toml|lock)|package(-lock)?\.json|pnpm-lock\.yaml|pnpm-workspace\.yaml|uv\.lock|pyproject\.toml|rust-toolchain\.toml)$|^scripts/ci/(check-lockfiles\.sh|lockfile-drift-allowlist\.txt)$"+TSX = "node docs/node_modules/tsx/dist/cli.mjs"+# CI's pnpm/action-setup reads the root package.json packageManager for docs.+DOCS_DEPS = ("docs/node_modules/tsx/dist/cli.mjs", "{pnpm} --dir docs install --frozen-lockfile")++def docs_install() -> str:+ return DOCS_DEPS[1].format(pnpm=pnpm_cmd(package_manager(ROOT)))+++# Docs generators that build Rust (or need extra tools) before they can compare.+DOCS_GENERATOR_NEEDS: Dict[str, Tuple[Tuple[str, ...], Tuple[Tuple[str, str], ...], bool]] = {+ # library: (tools, requires, heavy)+ "cua-driver": (("cargo",), (), True),+ "cua-sdk": (("cargo", "protoc"), (), True),+ "cua-cli": (("cargo", "protoc"), (), True),+ "cua-rust": (("cargo", "protoc", "rustup"), (), True),+ "cua-proto": (("buf",), (), False),+ "cua-sdk-python": (("uv",), (), False),+ "cua-bench": (("uv",), (), False),+ "cua-sdk-ts": ((), (("libs/cua/typescript/node_modules", "npm --prefix libs/cua/typescript ci --ignore-scripts"),), False),+ "lume": (("swift",), (), True),+}++# Web packages: (dir, trigger regex, pnpm version or "npm", install, [(id, title, cmd, fix)])+WEB_PACKAGES = [+ ("apps/cua-spaces-web", r"^(apps/cua-spaces-web/|apps/cua-spaces/core-wasm/|libs/cua/crates/cua-spaces-app-core/)",+ "11.20.0", "pnpm install --ignore-workspace --frozen-lockfile", [+ ("typecheck", "typecheck", "pnpm typecheck", None),+ ("design-lint", "design lint (drift guard)", "pnpm exec vitest run src/lib/design-lint.test.ts", None),+ ("test", "tests and bridge contract", "pnpm test", None),+ ]),+ ("apps/cua-spaces-desktop", r"^apps/cua-spaces-desktop/", None, "pnpm install --frozen-lockfile", [+ ("typecheck", "typecheck", "pnpm typecheck", None),+ ("test", "tests and router contract", "pnpm test", None),+ ]),+ ("apps/cua-spaces", r"^(apps/cua-spaces/(?!src-tauri/)|libs/cua/crates/cua-spaces-app-core/)", "9.0.4", "pnpm install --frozen-lockfile", [+ ("typecheck", "typecheck (builds the core wasm)", "pnpm typecheck", None),+ ("test", "vitest (app core parity)", "pnpm test", None),+ ]),+ ("libs/typescript", r"^libs/typescript/", None, "pnpm install --frozen-lockfile", [+ ("typecheck", "typecheck", "pnpm typecheck", None),+ ("format", "prettier", "pnpm format:check", "pnpm exec prettier --write ."),+ ]),+ ("libs/cua/typescript", r"^libs/cua/typescript/", "npm", "npm ci --ignore-scripts", [+ ("typecheck", "typecheck", "npm run typecheck", None),+ ]),+ ("libs/cua-driver/typescript", r"^libs/cua-driver/typescript/", "npm", "npm ci", [+ ("typecheck", "typecheck", "npm run typecheck", None),+ ]),+]++# Text CI must keep containing for each local check (checked by --verify-sync).+SYNC_REFS: List[Tuple[str, str, str]] = [+ # (check id prefix, workflow, text)+ ("rust-fmt", "ci-cua-sdk.yml", "-- --check"),+ ("rust-fmt", "ci-rust-format.yml", "cargo fmt --all -- --check"),+ ("rust-clippy", "ci-cua-sdk.yml", "--all-targets --features cua-daemon/test-fixtures,cua-media-client/media-decode,cua-spaces-cli/contrib-modal -- -D warnings"),+ ("rust-test", "ci-cua-sdk.yml", "CUA_ENV_TEST_SANDBOX: \"1\""),+ ("python", "ci-lint-python.yml", "uv run isort --check-only libs/python tests"),+ ("python", "ci-lint-python.yml", "uv run black --check libs/python tests"),+ ("python", "ci-lint-python.yml", "uv run ruff check libs/python tests"),+ ("web:apps/cua-spaces-web", "ci-cua-spaces-web.yml", "pnpm install --ignore-workspace --frozen-lockfile"),+ ("web:apps/cua-spaces-web", "ci-cua-spaces-web.yml", "run: pnpm typecheck"),+ ("web:apps/cua-spaces", "ci-cua-spaces.yml", "- run: pnpm typecheck"),+ ("web:libs/typescript", "ci-lint-typescript.yml", "pnpm -C libs/typescript typecheck"),+ ("web:libs/typescript", "ci-lint-typescript.yml", "pnpm -C libs/typescript format:check"),+ ("docs-ref", "ci-check-docs.yml", "runner.ts --changed-files-file"),+ ("docs-ref", "ci-check-docs.yml", "runner.ts --library ${{ matrix.library }} --check"),+]+++def static_checks(changes: Changes, ctx: "Context") -> List[Check]:+ checks: List[Check] = []+ add = checks.append++ if changes.touches(r"\.(rs|py|ts|tsx|js|mjs|swift|sh)$|^scripts/spdx-headers\.py$"):+ add(Check("spdx", "SPDX headers (FSL)", "python3 scripts/spdx-headers.py --fsl --check",+ fix="python3 scripts/spdx-headers.py --fsl --apply", safe_fix=True,+ ci=(("ci-spdx-headers.yml", "python3 scripts/spdx-headers.py --fsl --check"),)))+ if changes.touches(r"^libs/cua-driver/.*\.(rs|swift)$"):+ add(Check("spdx-driver", "SPDX headers (Cua Driver, MIT)", "python3 scripts/spdx-headers.py --check libs/cua-driver",+ fix="python3 scripts/spdx-headers.py --apply libs/cua-driver", safe_fix=True,+ ci=(("ci-spdx-headers.yml", "python3 scripts/spdx-headers.py --check libs/cua-driver"),)))++ lock_hits = changes.touches(LOCKFILE_RX)+ if lock_hits:+ kinds = set()+ for f in lock_hits:+ if changes.full or re.search(r"(Cargo\.(toml|lock)|rust-toolchain\.toml)$", f):+ kinds.add("cargo")+ if changes.full or re.search(r"(package\.json|pnpm-lock\.yaml|pnpm-workspace\.yaml)$", f):+ kinds.update({"pnpm", "npm"})+ if changes.full or re.search(r"(package-lock\.json)$", f):+ kinds.add("npm")+ if changes.full or re.search(r"(pyproject\.toml|uv\.lock)$", f):+ kinds.add("uv")+ if re.search(r"^scripts/ci/", f):+ kinds.update({"cargo", "pnpm", "npm", "uv"})+ tools = {"cargo": "cargo", "pnpm": "pnpm", "npm": "npm", "uv": "uv"}+ add(Check("lockfiles", f"lockfiles match manifests ({', '.join(sorted(kinds))})",+ "scripts/ci/check-lockfiles.sh " + " ".join(f"--kind {k}" for k in sorted(kinds)),+ fix_from_output=r"^FAIL .*?; fix: (.+)$", safe_fix=True,+ tools=tuple(sorted(tools[k] for k in kinds)), group="lockfiles",+ ci=(("ci-lockfiles.yml", "scripts/ci/check-lockfiles.sh"),)))++ if changes.touches(r"^\.github/(workflows|actions)/|^\.github/actionlint\.yaml$"):+ add(Check("actionlint", "actionlint (workflows)", "actionlint -shellcheck= -pyflakes=",+ tools=("actionlint",), hint="install: brew install actionlint (CI pins v1.7.12)",+ ci=(("ci-lint-workflows.yml", "./actionlint -shellcheck= -pyflakes="),)))++ # Repo-wide gate (no path filter in CI): every image ref comes from the catalog.+ if changes.files or changes.full:+ add(Check("image-refs", "image refs come from the catalog",+ "python3 scripts/images/check-image-refs.py && python3 scripts/images/gen-image-constants.py --check "+ "&& python3 scripts/images/record-image-sizes.py --check",+ hint="reference images through the catalog (scripts/images/README.md) or allowlist them in scripts/images/image-refs-allowlist.json; stale constants: python3 scripts/images/gen-image-constants.py",+ ci=(("ci-check-image-refs.yml", "python3 scripts/images/check-image-refs.py"),+ ("ci-check-image-refs.yml", "python3 scripts/images/gen-image-constants.py --check"))))+ if changes.touches(r"^(scripts/images/|libs/images/)"):+ add(Check("image-tests", "image tooling unit tests", "python3 -m unittest discover -s scripts/images/tests",+ ci=(("ci-check-image-refs.yml", "python3 -m unittest discover -s scripts/images/tests"),)))++ if changes.touches(r"^docs/content/|^docs/code-block-(baseline|policy)\.json$|^tests/e2e/cua-sdk/docs/"):+ add(Check("docs-code-blocks", "docs code blocks have a disposition",+ f'b="$(mktemp)"; git show {changes.base}:docs/code-block-baseline.json > "$b" 2>/dev/null '+ f'&& args=(--baseline-base "$b") || args=(); python3 tests/e2e/cua-sdk/docs/extract.py --lint "${{args[@]}}"; rc=$?; rm -f "$b"; exit $rc',+ hint='give each new fence a disposition: test="<lane>" id="<id>", skip="<reason>" or output (tests/e2e/cua-sdk/docs/README.md)',+ ci=(("ci-check-docs-links.yml", "python3 tests/e2e/cua-sdk/docs/extract.py --lint"),)))++ docs_img = changes.touches(r"^(docs/|skills/|README\.md$|libs/.*README\.md$|libs/cua/crates/cua-image/src/(canonical|resolve)\.rs$|scripts/images/(check-docs-image-refs|doctor_ledger)\.py$|scripts/images/docs-image-refs-baseline\.json$)")+ if docs_img:+ c = Check("docs-image-refs", "docs image refs have passing doctor reports",+ f'set -e; l="$(mktemp -d)"; trap \'rm -rf "$l"\' EXIT; '+ f'git fetch -q origin image-doctor-ledger 2>/dev/null && git archive FETCH_HEAD | tar -x -C "$l" || true; '+ f'b="$l/.base.json"; args=(--ledger "$l"); git show {changes.base}:scripts/images/docs-image-refs-baseline.json > "$b" 2>/dev/null && args+=(--baseline-base "$b"); '+ f'python3 scripts/images/check-docs-image-refs.py "${{args[@]}}"',+ tools=("crane",), hint="install crane: brew install crane",+ ci=(("ci-check-docs-image-refs.yml", "python3 scripts/images/check-docs-image-refs.py"),))+ add(c)++ # Generated reference docs: the docs runner itself says which generators a change affects.+ if changes.files or changes.full:+ for lib in ctx.docs_generators(changes):+ tools, requires, heavy = DOCS_GENERATOR_NEEDS.get(lib, ((), (), False))+ add(Check(f"docs-ref:{lib}", f"generated reference is current ({lib})",+ f"{TSX} scripts/docs-generators/runner.ts --library {lib} --check",+ fix=f"{TSX} scripts/docs-generators/runner.ts --library {lib} # = pnpm --dir docs docs:generate:{lib}",+ safe_fix=True, tools=("node",) + tools, requires=((DOCS_DEPS[0], docs_install()),) + requires,+ heavy=heavy, tier="build" if heavy else "quick",+ group="cargo:libs/cua" if lib in ("cua-sdk", "cua-cli", "cua-rust") else ("cargo:libs/cua-driver/rust" if lib == "cua-driver" else None),+ ci=(("ci-check-docs.yml", "runner.ts --library ${{ matrix.library }} --check"),)))++ # Generated bindings and contract manifests.+ if changes.touches(r"^libs/cua/(crates/cua-sdk/|crates/cua-bindgen/|VERSION$|scripts/generate-uniffi-bindings\.mjs$|scripts/uniffi-python-postprocess\.mjs$|typescript/package-lock\.json$|python/|swift/|kotlin/|typescript/src/generated/|Cargo\.lock$)"):+ add(Check("sdk-bindings", "cua SDK UniFFI bindings are current (Python, Swift, Kotlin, TS)",+ "node libs/cua/scripts/generate-uniffi-bindings.mjs --check",+ fix="node libs/cua/scripts/generate-uniffi-bindings.mjs", safe_fix=True,+ tools=("node", "cargo"), requires=(("libs/cua/typescript/node_modules", "npm --prefix libs/cua/typescript ci --ignore-scripts"),),+ group="cargo:libs/cua", heavy=True, tier="build",+ ci=(("ci-cua-sdk.yml", "node libs/cua/scripts/generate-uniffi-bindings.mjs --check"),)))+ if ctx.crate_trigger(changes, "libs/cua", ["cua-spaces-contract", "cua-spaces"], r"^libs/cua/spaces-contract/"):+ add(Check("spaces-contract", "Spaces contract manifest and control fixtures are current",+ "cargo run --locked -q -p cua-spaces-contract --bin cua-spaces-contract-gen -- --check && "+ "cargo run --locked -q -p cua-spaces --bin cua-spaces-control-fixtures -- --check",+ cwd="libs/cua",+ fix="(cd libs/cua && cargo run --locked -q -p cua-spaces-contract --bin cua-spaces-contract-gen && "+ "cargo run --locked -q -p cua-spaces --bin cua-spaces-control-fixtures)",+ safe_fix=True, tools=("cargo", "protoc"), group="cargo:libs/cua", tier="build",+ ci=(("ci-cua-sdk.yml", "cargo run --locked -q -p cua-spaces-contract --bin cua-spaces-contract-gen -- --check"),+ ("ci-cua-sdk.yml", "cargo run --locked -q -p cua-spaces --bin cua-spaces-control-fixtures -- --check"))))+ if ctx.crate_trigger(changes, "libs/cua-driver/rust", ["cua-driver-contract"], r"^libs/cua-driver/contract/"):+ add(Check("driver-contract", "Cua Driver contract files are current",+ "cargo run --locked -q -p cua-driver-contract --bin cua-contract-gen -- all --check", cwd="libs/cua-driver/rust",+ fix="(cd libs/cua-driver/rust && cargo run --locked -q -p cua-driver-contract --bin cua-contract-gen -- all)",+ safe_fix=True, tools=("cargo",), group="cargo:libs/cua-driver/rust", tier="build",+ ci=(("ci-cua-driver-contract-clients.yml", "cargo run --locked -p cua-driver-contract --bin cua-contract-gen -- all --check"),)))+ if ctx.crate_trigger(changes, "libs/cua-driver/rust", ["cua-driver-sdk", "cua-driver-bindgen"],+ r"^libs/cua-driver/(scripts/generate-uniffi-bindings\.mjs$|python/src/cua_driver/_native|typescript/src/native/)"):+ add(Check("driver-bindings", "Cua Driver UniFFI bindings are current (release build)",+ "node scripts/generate-uniffi-bindings.mjs --check", cwd="libs/cua-driver",+ fix="(cd libs/cua-driver && node scripts/generate-uniffi-bindings.mjs)", safe_fix=True,+ tools=("node", "cargo"), requires=(("libs/cua-driver/typescript/node_modules", "npm --prefix libs/cua-driver/typescript ci"),),+ group="cargo:libs/cua-driver/rust", heavy=True, tier="build",+ ci=(("ci-cua-driver-contract-clients.yml", "node scripts/generate-uniffi-bindings.mjs --check"),)))+ # The Spaces app library (cua-spaces-ffi) re-exports the SDK; only UniFFI crates change its interface.+ if changes.touches(r"^libs/cua/crates/(cua-sdk|cua-spaces-ffi)/.*\.rs$|^apps/cua-spaces-desktop/src/native/generated/|^libs/spaces-app-swift/"):+ desk_pnpm = pnpm_cmd(package_manager(ROOT / "apps/cua-spaces-desktop"))+ add(Check("spaces-app-bindings", "Electron app bindings are current (release build of cua-spaces-ffi)",+ f"{desk_pnpm} native -- --check-bindings", cwd="apps/cua-spaces-desktop",+ fix=f"(cd apps/cua-spaces-desktop && {desk_pnpm} native -- --bindings)", safe_fix=True,+ tools=("node", "cargo"), requires=(("apps/cua-spaces-desktop/node_modules", f"(cd apps/cua-spaces-desktop && {desk_pnpm} install --frozen-lockfile)"),),+ group="cargo:libs/cua", heavy=True, tier="build",+ ci=(("ci-cua-spaces-macos.yml", "pnpm native -- --skip-build --check-bindings"),)))+ if sys.platform == "darwin":+ add(Check("spaces-swift-bindings", "SwiftUI app binding is current (release build of cua-spaces-ffi)",+ "node libs/spaces-app-swift/scripts/generate-bindings.mjs --check",+ fix="node libs/spaces-app-swift/scripts/generate-bindings.mjs", safe_fix=True,+ tools=("node", "cargo"), group="cargo:libs/cua", heavy=True, tier="build",+ ci=(("ci-cua-spaces-macos.yml", "node libs/spaces-app-swift/scripts/generate-bindings.mjs --check --skip-build"),)))++ # Self-tests for this tool and the CI helper scripts.+ if changes.touches(r"^(bin/ci$|scripts/ci/|\.github/workflows/)"):+ add(Check("bin-ci-tests", "bin/ci tests and workflow sync", "python3 -m unittest discover -s scripts/ci/tests -q && python3 bin/ci --verify-sync",+ ci=(("ci-test-scripts.yml", "python3 -m unittest discover -s scripts/ci/tests"),)))+ if changes.touches(r"^\.github/scripts/"):+ add(Check("gh-scripts-tests", ".github/scripts tests",+ "cd .github/scripts && uv run --no-project -q --with pytest --with cryptography --with jsonschema --with pyyaml --with toml pytest tests/ -q",+ tools=("uv",), tier="build", ci=(("ci-test-scripts.yml", "pytest tests/ -v"),)))+ return checks+++def python_checks(changes: Changes) -> List[Check]:+ # The same command and paths as CI (whole tree, seconds with the tools'+ # caches): a formatter bump in uv.lock can fail files the change never touched.+ if not changes.touches(r"^(libs/python/|tests/).*\.pyi?$|^(pyproject\.toml|uv\.lock)$"):+ return []+ paths = "libs/python tests"+ return [Check("python", "Python isort, black, ruff (libs/python, tests)",+ f"uv run -q isort --check-only {paths} && uv run -q black --check {paths} && uv run -q ruff check {paths}",+ fix=f"uv run -q isort {paths} && uv run -q black {paths} && uv run -q ruff check --fix {paths}",+ safe_fix=True, tools=("uv",), group="uv",+ ci=(("ci-lint-python.yml", "uv run isort --check-only libs/python tests"),+ ("ci-lint-python.yml", "uv run black --check libs/python tests"),+ ("ci-lint-python.yml", "uv run ruff check libs/python tests")))]+++def web_checks(changes: Changes) -> List[Check]:+ out: List[Check] = []+ for pkg, rx, pm, install, steps in WEB_PACKAGES:+ if not (ROOT / pkg / "package.json").exists() or not changes.touches(rx):+ continue+ if pm == "npm":+ runner, inst = "npm", install+ else:+ runner = pnpm_cmd(pm or package_manager(ROOT / pkg))+ inst = install.replace("pnpm", runner, 1)+ for sid, title, cmd, fix in steps:+ cmd = cmd.replace("pnpm", runner, 1) if cmd.startswith("pnpm") else cmd+ heavy = "core wasm" in title or "parity" in title or pkg == "apps/cua-spaces"+ out.append(Check(f"web:{pkg}:{sid}", f"{pkg}: {title}", cmd, cwd=pkg,+ fix=f"(cd {pkg} && {fix.replace('pnpm', runner, 1)})" if fix else None, safe_fix=bool(fix),+ tools=("node",) + (("pnpm",) if runner == "pnpm" else ()),+ requires=((f"{pkg}/node_modules", f"(cd {pkg} && {inst})"),),+ group=f"web:{pkg}", heavy=heavy, tier="build" if sid == "test" or heavy else "quick"))+ return out+++UNIX_ONLY_RX = re.compile(r"std::os::unix|\bUnix(Stream|Listener|Datagram)\b|\blibc::(?!c_)|\bnix::|PermissionsExt|\.mode\(0o|std::os::fd|symlink\(")+++def unix_api_scan(changes: Changes) -> List[str]:+ """Added lines that use Unix-only APIs in files with no cfg(unix) at all."""+ hits = []+ for f in changes.existing(r"\.rs$"):+ text = (ROOT / f).read_text(errors="replace")+ if re.search(r"cfg\((all\()?(unix|not\(windows\)|not\(target_os = \"windows\"\)|target_os = \"(linux|macos)\")", text):+ continue+ if re.search(r"(^|/)(unix|linux|macos|darwin)[^/]*\.rs$|/platform-(linux|macos)/", f):+ continue+ diff = git("diff", "-U0", changes.base, "--", f)+ for line in diff.splitlines():+ if line.startswith("+") and not line.startswith("+++") and UNIX_ONLY_RX.search(line):+ hits.append(f"{f}: {line[1:].strip()[:100]}")+ return hits+++def classify_xwin(log: str, rc: int) -> Tuple[str, str]:+ """A C dependency that cannot be cross-built here is not a finding.++ `--keep-going` checks every crate that does not need it; only rustc+ errors in those crates fail the check.+ """+ if rc == 0:+ return "pass", ""+ if re.search(r"could not compile `[^`]+`", log):+ return "FAIL", ""+ blocked = sorted(set(re.findall(r"failed to run custom build command for `([^`]+)`", log)))+ if blocked:+ return "warn", (f"could not cross-build {', '.join(blocked)} for Windows on this machine; "+ "the crates that need it were not checked, every other crate passed")+ return "FAIL", ""+++def rust_checks(changes: Changes, ctx: "Context") -> List[Check]:+ out: List[Check] = []+ if not have("cargo"):+ return [Check("rust", "Rust checks", "true", skip_reason="cargo not installed")] if changes.existing(r"\.rs$") else []+ if changes.full:+ crates = {}+ for root in ("libs/cua", "libs/cua-driver/rust", "libs/cua-spacesd"):+ ws = workspace_of(root, ctx.rust_env)+ if ws:+ crates[ws.root] = (ws, set(ws.members))+ else:+ crates = changed_crates(changes, ctx.rust_env)+ for root, (ws, names) in sorted(crates.items()):+ names = sorted(names)+ plist = " ".join(f"-p {n}" for n in names)+ label = f"{root}: {', '.join(names) if len(names) <= 4 else f'{len(names)} crates'}"+ group = f"cargo:{root}"+ out.append(Check(f"rust-fmt:{root}", f"rustfmt ({label})", f"cargo fmt {plist} -- --check", cwd=root,+ fix=f"(cd {root} && cargo fmt {plist})", safe_fix=True, tools=("cargo",),+ ci=(("ci-rust-format.yml", "cargo fmt --all -- --check"),)))+ lint = names + ([] if changes.full else dependents(ws, names))+ if not changes.full and len(lint) > len(names) + 12: # a core crate: lint its direct dependents only+ direct = sorted({p for p, deps in ws.path_deps.items() if set(deps) & set(names)} - set(names))+ ctx.notes.append(f"{root}: {len(lint) - len(names)} crates depend on the {len(names)} you changed; linting the {len(direct)} direct ones (bin/ci --full for all)")+ lint = names + direct+ lflags = "--workspace" if changes.full else " ".join(f"-p {n}" for n in sorted(set(lint)))+ llabel = f"{root}: {len(set(lint))} crates" if len(set(lint)) > 4 else label+ out.append(Check(f"rust-clippy:{root}", f"clippy -D warnings ({llabel})",+ f"cargo clippy --locked {lflags} --all-targets -- -D warnings", cwd=root,+ tools=("cargo",), group=group, tier="build",+ hint="fix the warnings above; `cargo clippy --fix --allow-dirty` applies the machine-applicable ones"))+ lane = windows_lane(root)+ if lane:+ args, lane_pkgs = lane+ closure = deps_closure(ws, lane_pkgs) if lane_pkgs else set(ws.members)+ if changes.full or set(names) & closure:+ sub = WINDOWS_LANES[root][2]+ if "--locked" not in args:+ args += " --locked"+ if sub == "check":+ cmd = f"cargo xwin check --keep-going --target x86_64-pc-windows-msvc {args}"+ else:+ cmd = f"cargo xwin clippy --keep-going --target x86_64-pc-windows-msvc {args}"+ wf = WINDOWS_LANES[root][0]+ if ctx.xwin:+ out.append(Check(f"rust-windows:{root}", f"Windows {sub}, cross-compiled (the {wf} Windows lane)", cmd,+ cwd=root, tools=("cargo",), group="xwin", tier="build", heavy=True, classify=classify_xwin,+ hint="code that only Unix uses must be #[cfg(unix)] too (or #[cfg_attr(windows, allow(dead_code))] with a reason)"))+ else:+ hits = unix_api_scan(changes)+ out.append(Check(f"rust-windows:{root}", "Windows cfg (grep for Unix-only APIs; cargo-xwin not installed)",+ "printf '%s\\n' " + " ".join(shlex.quote(h) for h in hits) + " && exit 1" if hits else "true",+ advisory=True,+ hint="these lines use Unix-only APIs outside #[cfg(unix)]; for a real Windows compile: "+ "cargo install cargo-xwin && rustup target add x86_64-pc-windows-msvc"))+ tflags = "--workspace" if changes.full else plist+ out.append(Check(f"rust-test:{root}", f"unit tests ({label})",+ f"cargo test --locked --no-fail-fast {tflags} -- --test-threads=4", cwd=root,+ tools=("cargo",), group=group, tier="build",+ env={"CUA_ENV_TEST_SANDBOX": "1", "RUST_MIN_STACK": "8388608"}, isolate_home=True))+ return out+++# ---------------------------------------------------------------- context and plan+++class Context:+ def __init__(self, args: argparse.Namespace) -> None:+ self.args = args+ self.notes: List[str] = []+ self.rust_env = rust_env(self.notes)+ self.xwin = have("cargo") and subprocess.run(+ ["cargo", "xwin", "--version"], capture_output=True, env={**os.environ, **self.rust_env}+ ).returncode == 0+ self.docs_ready = (ROOT / DOCS_DEPS[0]).exists()++ def env(self) -> Dict[str, str]:+ return {**os.environ, **self.rust_env, "CUA_TELEMETRY": "0", "DO_NOT_TRACK": "1"}++ def ensure_docs_deps(self) -> bool:+ if self.docs_ready:+ return True+ if self.args.no_install or not have("node"):+ return False+ print(f"bin/ci: installing docs dependencies ({docs_install()}) to route the docs generators ...", flush=True)+ ok = subprocess.run(docs_install(), shell=True, cwd=str(ROOT), stdout=subprocess.DEVNULL, env=self.env()).returncode == 0+ self.docs_ready = ok and (ROOT / DOCS_DEPS[0]).exists()+ return self.docs_ready++ def docs_generators(self, changes: Changes) -> List[str]:+ if changes.full:+ return ["cua-driver", "cua-bench", "sandbox", "cua-cli", "cua-sdk", "cua-sdk-python", "cua-sdk-ts", "cua-rust", "cua-proto", "spaces"] + (["lume"] if sys.platform == "darwin" else [])+ if not self.ensure_docs_deps():+ self.notes.append(f"docs generators not routed: run {docs_install()}")+ return []+ with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as fh:+ fh.write("\n".join(changes.files) + "\n")+ try:+ out = subprocess.run(f"{TSX} scripts/docs-generators/runner.ts --changed-files-file {shlex.quote(fh.name)}",+ shell=True, cwd=str(ROOT), capture_output=True, text=True, env=self.env())+ finally:+ os.unlink(fh.name)+ if out.returncode != 0:+ self.notes.append("the docs generator router failed: " + out.stderr.strip()[-200:])+ return []+ return out.stdout.split()++ def crate_trigger(self, changes: Changes, ws_root: str, roots: List[str], path_rx: str) -> bool:+ if changes.full or changes.touches(path_rx):+ return True+ if not have("cargo"):+ return False+ ws = workspace_of(ws_root, self.rust_env)+ if not ws:+ return False+ closure = deps_closure(ws, roots)+ crates = changed_crates(changes, self.rust_env)+ return bool(crates.get(ws.root, (None, set()))[1] & closure)+++def build_plan(changes: Changes, ctx: Context) -> List[Check]:+ checks = static_checks(changes, ctx) + rust_checks(changes, ctx) + python_checks(changes) + web_checks(changes)+ seen, plan = set(), []+ for c in checks:+ if c.id not in seen:+ seen.add(c.id)+ plan.append(c)+ if ctx.args.only:+ wanted = [w.strip() for w in ctx.args.only.split(",") if w.strip()]+ plan = [c for c in plan if any(c.id == w or c.id.startswith(w + ":") or c.id.startswith(w) for w in wanted)]+ for c in plan:+ if c.skip_reason:+ continue+ if ctx.args.quick and c.tier != "quick":+ c.skip_reason = "--quick"+ continue+ missing = [t for t in c.tools if not have(t)]+ if missing:+ c.skip_reason = f"{', '.join(missing)} not installed" + (f"; {c.hint}" if c.hint and "install" in c.hint else "")+ return plan+++# ---------------------------------------------------------------- running++ERROR_RX = re.compile(r"(error|Diff in|would reformat|FAIL|stale|out of sync|drift|panicked|not up to date|missing|no disposition|differ|warning: unused|✗|❌)", re.I)+++def excerpt(log: Path, limit: int = 14) -> List[str]:+ lines = [l.rstrip() for l in log.read_text(errors="replace").splitlines() if l.strip()]+ rustc = [l for l in lines if re.match(r"(error(\[E\d+\])?: (?!failed to run custom build|could not compile|build failed)|\s+--> )", l)]+ if rustc:+ return [l[:220] for l in rustc[:limit]]+ hits = [l for l in lines if ERROR_RX.search(l) and not l.lstrip().startswith(("Compiling", "Checking", "Downloaded", "test ", "Running"))]+ pick = hits[:limit] if hits else lines[-limit:]+ return [l[:220] for l in pick]+++class Runner:+ def __init__(self, ctx: Context, logdir: Path, quiet: bool = False) -> None:+ self.ctx = ctx+ self.quiet = quiet+ self.logdir = logdir+ self.locks: Dict[str, threading.Lock] = {}+ self.print_lock = threading.Lock()+ self.installs: Dict[str, threading.Lock] = {}++ def _lock(self, name: Optional[str], table: Dict[str, threading.Lock]) -> threading.Lock:+ with self.print_lock:+ return table.setdefault(name or "", threading.Lock()) if name else threading.Lock()++ def _prepare(self, c: Check, log) -> Optional[str]:+ for path, how in c.requires:+ with self._lock(path, self.installs):+ if (ROOT / path).exists():+ continue+ if self.ctx.args.no_install:+ return f"missing {path}: run {how}"+ log.write(f"$ {how}\n".encode())+ log.flush()+ rc = subprocess.run(how, shell=True, cwd=str(ROOT), stdout=log, stderr=subprocess.STDOUT, env=self.ctx.env(), executable="/bin/bash").returncode+ if rc != 0 or not (ROOT / path).exists():+ return f"could not install {path} ({how}); see the log"+ return None++ def run(self, c: Check) -> Result:+ if c.skip_reason:+ return Result(c, "skip", detail=c.skip_reason)+ logfile = self.logdir / (re.sub(r"[^A-Za-z0-9._-]+", "_", c.id) + ".log")+ start = time.monotonic()+ with self._lock(c.group, self.locks):+ with open(logfile, "wb") as log:+ problem = self._prepare(c, log)+ if problem:+ return Result(c, "skip", time.monotonic() - start, logfile, problem)+ log.write(f"$ (cd {c.cwd} && {c.cmd})\n".encode())+ log.flush()+ env = {**self.ctx.env(), **c.env}+ home = None+ if c.isolate_home:+ # Tests that read the host's apps or config pass on a clean+ # runner and fail here; keep the toolchains where they are.+ real = Path.home()+ env.setdefault("CARGO_HOME", str(real / ".cargo"))+ env.setdefault("RUSTUP_HOME", str(real / ".rustup"))+ home = tempfile.mkdtemp(prefix="bin-ci-home-")+ env["HOME"] = home+ try:+ rc = subprocess.run(["bash", "-o", "pipefail", "-c", c.cmd], cwd=str(ROOT / c.cwd), stdout=log, stderr=subprocess.STDOUT,+ stdin=subprocess.DEVNULL, env=env).returncode+ finally:+ if home:+ shutil.rmtree(home, ignore_errors=True)+ status = "pass" if rc == 0 else ("warn" if c.advisory else "FAIL")+ detail = ""+ if c.classify:+ status, detail = c.classify(logfile.read_text(errors="replace"), rc)+ res = Result(c, status, time.monotonic() - start, logfile, detail)+ if not self.quiet:+ self.report(res)+ return res++ def report(self, r: Result) -> None:+ with self.print_lock:+ print_result(r)+++def fix_commands(r: Result) -> List[str]:+ c = r.check+ cmds = []+ if c.fix_from_output and r.log and r.log.exists():+ rx = re.compile(c.fix_from_output)+ for line in r.log.read_text(errors="replace").splitlines():+ m = rx.search(line.strip())+ if m and m.group(1) not in cmds:+ cmds.append(m.group(1))+ if c.fix and not cmds:+ cmds.append(c.fix)+ return cmds+++def fmt_secs(s: float) -> str:+ return f"{s:.1f}s" if s < 60 else f"{int(s // 60)}m{int(s % 60):02d}s"+++def print_result(r: Result) -> None:+ tag = {"pass": color("ok ", "32"), "FAIL": color("FAIL", "31;1"), "skip": color("skip", "33"),+ "fixed": color("fixed", "36"), "warn": color("warn", "33")}[r.status]+ secs = fmt_secs(r.seconds) if r.seconds else ""+ print(f" {tag} {r.check.title} {color(secs, '2')}".rstrip(), flush=True)+ if r.status == "skip":+ print(f" {r.detail}")+ if r.status == "warn" and r.detail:+ print(f" {r.detail}")+ elif r.status in ("FAIL", "warn") and r.log:+ for line in excerpt(r.log):+ print(f" | {line}")+ fixes = fix_commands(r)+ for f in fixes:+ print(f" {color('fix:', '1')} {f}")+ if not fixes and r.check.hint:+ print(f" {color('fix:', '1')} {r.check.hint}")+ rel = os.path.relpath(r.log, ROOT)+ print(f" log: {rel if not rel.startswith('..') else r.log}")+ if r.status == "fixed":+ print(f" {r.detail}")+++def run_plan(plan: List[Check], ctx: Context, logdir: Path) -> List[Result]:+ runner = Runner(ctx, logdir)+ results: Dict[str, Result] = {}+ order = sorted(plan, key=lambda c: (c.skip_reason is None, c.heavy, c.tier != "quick"))+ for c in order:+ if c.skip_reason:+ results[c.id] = Result(c, "skip", detail=c.skip_reason)+ print_result(results[c.id])+ jobs = ctx.args.jobs or max(2, min(6, (os.cpu_count() or 4) // 2))+ with concurrent.futures.ThreadPoolExecutor(max_workers=jobs) as pool:+ futures = {pool.submit(runner.run, c): c for c in order if not c.skip_reason}+ for fut in concurrent.futures.as_completed(futures):+ r = fut.result()+ results[r.check.id] = r+ if r.status == "skip" and r.log is not None:+ runner.report(r)+ return [results[c.id] for c in plan]+++def apply_fixes(results: List[Result], ctx: Context, logdir: Path) -> List[Result]:+ out = []+ for r in results:+ cmds = fix_commands(r) if r.status == "FAIL" and r.check.safe_fix else []+ if not cmds:+ out.append(r)+ continue+ print(f" {color('fixing', '36')} {r.check.title}: {'; '.join(cmds)}", flush=True)+ ok = all(subprocess.run(["bash", "-c", cmd], cwd=str(ROOT), env=ctx.env()).returncode == 0 for cmd in cmds)+ again = Runner(ctx, logdir, quiet=True).run(r.check) if ok else r+ if again.status == "pass":+ again.status, again.detail = "fixed", "fixed; review and commit the changes"+ print_result(again)+ out.append(again)+ return out+++# ---------------------------------------------------------------- state, sign-off, hook+++def state_dir() -> Path:+ d = Path(git("rev-parse", "--absolute-git-dir").strip()) / "bin-ci"+ (d / "logs").mkdir(parents=True, exist_ok=True)+ return d+++def worktree_clean() -> bool:+ return not git("status", "--porcelain", "--untracked-files=normal").strip()+++def save_state(results: List[Result], mode: str, seconds: float) -> dict:+ data = {+ "head": git("rev-parse", "HEAD").strip(), "clean": worktree_clean(), "mode": mode,+ "seconds": round(seconds, 1),+ "passed": [r.check.id for r in results if r.status in ("pass", "fixed")],+ "failed": [r.check.id for r in results if r.status == "FAIL"],+ "skipped": [r.check.id for r in results if r.status == "skip"],+ "at": int(time.time()),+ }+ (state_dir() / "last-run.json").write_text(json.dumps(data, indent=2))+ return data+++def github_repo() -> Optional[str]:+ url = git("remote", "get-url", "origin").strip()+ m = re.search(r"github\.com[:/]([^/]+/[^/]+?)(\.git)?$", url)+ return m.group(1) if m else None+++def signoff(state: dict) -> int:+ if state["failed"] or not state["passed"]:+ print("bin/ci: not signing off: the run is not green")+ return 1+ if not state["clean"]:+ print("bin/ci: not signing off: uncommitted changes; commit them and run again")+ return 1+ if state["skipped"]:+ print(f"bin/ci: not signing off: {len(state['skipped'])} check(s) skipped ({', '.join(state['skipped'])}); install what they need")+ return 1+ repo = github_repo()+ if not repo or not have("gh"):+ print("bin/ci: not signing off: needs the gh CLI and a GitHub origin")+ return 1+ desc = f"bin/ci {state['mode']}: {len(state['passed'])} checks passed in {fmt_secs(state['seconds'])} ({sys.platform})"+ out = subprocess.run(["gh", "api", "-X", "POST", f"repos/{repo}/statuses/{state['head']}", "-f", "state=success",+ "-f", f"context={STATUS_CONTEXT}", "-f", f"description={desc[:140]}"], capture_output=True, text=True)+ if out.returncode != 0:+ if "No commit found" in out.stdout + out.stderr or "422" in out.stderr:+ print(f"bin/ci: {state['head'][:10]} is not on GitHub yet; push, then run `bin/ci --signoff` again (it reuses this green run)")+ else:+ print("bin/ci: gh api failed: " + (out.stderr.strip() or out.stdout.strip())[:300])+ return 1+ print(f"bin/ci: set {STATUS_CONTEXT} = success on {state['head'][:10]} ({desc})")+ return 0+++HOOK_SCRIPT = f"""#!/bin/sh+{HOOK_MARKER} (installed by `bin/ci --install-hook`; remove: `bin/ci --uninstall-hook`).+# Runs the checks your push affects. Skip it once: CUA_SKIP_LOCAL_CI=1 git push ...+# (`git push --no-verify` also skips it, and every other pre-push hook with it.)+here=$(cd "$(dirname "$0")" && pwd)+refs=$(mktemp) || exit 1+trap 'rm -f "$refs"' EXIT+cat > "$refs"+if [ -x "$here/{CHAINED_HOOK}" ]; then+ "$here/{CHAINED_HOOK}" "$@" < "$refs" || exit $?+fi+if [ "${{CUA_SKIP_LOCAL_CI:-0}}" = 1 ]; then+ echo "bin/ci: skipped (CUA_SKIP_LOCAL_CI=1)" >&2+ exit 0+fi+root=$(git rev-parse --show-toplevel 2>/dev/null) || exit 0+[ -x "$root/bin/ci" ] || exit 0+"$root/bin/ci" --hook ${{CUA_LOCAL_CI_ARGS:-}} < "$refs"+"""+++def install_hook(force: bool) -> int:+ hooks = Path(git("rev-parse", "--path-format=absolute", "--git-common-dir").strip()) / "hooks"+ hooks.mkdir(exist_ok=True)+ hook = hooks / "pre-push"+ if hook.exists() and HOOK_MARKER not in hook.read_text(errors="replace"):+ chained = hooks / CHAINED_HOOK+ if chained.exists() and not force:+ print(f"bin/ci: {hook} exists and so does {chained}; pass --force to replace {hook}")+ return 1+ hook.rename(chained)+ print(f"bin/ci: kept your existing pre-push hook as {chained}; it still runs first")+ hook.write_text(HOOK_SCRIPT)+ hook.chmod(0o755)+ print(f"bin/ci: installed {hook}")+ hooks_path = git("config", "--get", "core.hooksPath").strip()+ if hooks_path:+ dispatch = Path(os.path.expanduser(hooks_path)) / "pre-push"+ chains = dispatch.exists() and "git-common-dir" in dispatch.read_text(errors="replace") + (+ (dispatch.parent / "_dispatch").read_text(errors="replace") if (dispatch.parent / "_dispatch").exists() else "")+ if chains:+ print(f"bin/ci: core.hooksPath is {hooks_path}; its pre-push runs first and then chains to this hook")+ else:+ print(f"bin/ci: warning: core.hooksPath is {hooks_path}, so git will not run {hook}. "+ f"Call it from {dispatch}, or run bin/ci yourself before pushing.")+ return 0+++def uninstall_hook() -> int:+ hooks = Path(git("rev-parse", "--path-format=absolute", "--git-common-dir").strip()) / "hooks"+ hook = hooks / "pre-push"+ if not hook.exists() or HOOK_MARKER not in hook.read_text(errors="replace"):+ print("bin/ci: no bin/ci pre-push hook installed")+ return 0+ hook.unlink()+ chained = hooks / CHAINED_HOOK+ if chained.exists():+ chained.rename(hook)+ print(f"bin/ci: removed the hook and restored your previous {hook}")+ else:+ print(f"bin/ci: removed {hook}")+ return 0+++def hook_refs_are_deletions(stdin_text: str) -> bool:+ lines = [l.split() for l in stdin_text.splitlines() if l.strip()]+ return bool(lines) and all(len(p) >= 2 and set(p[1]) == {"0"} for p in lines)+++# ---------------------------------------------------------------- sync check+++def all_check_refs() -> List[Tuple[str, str, str]]:+ """Every (check id, workflow, text) pair, from a full plan without running anything."""+ class Dummy:+ notes: List[str] = []+ rust_env: Dict[str, str] = {}++ def docs_generators(self, changes):+ return ["cua-sdk"]++ def crate_trigger(self, *a):+ return True++ full = Changes("HEAD", "HEAD", ["x.rs", "x.py", ".github/workflows/x.yml", "Cargo.toml", "docs/content/x.mdx", "libs/cua/crates/cua-sdk/x.rs", ".github/scripts/x.py", "libs/cua-driver/x.rs", "scripts/images/x.py", "bin/ci"], full=False)+ refs = []+ for c in static_checks(full, Dummy()): # type: ignore[arg-type]+ refs.extend((c.id, wf, text) for wf, text in c.ci)+ for c in python_checks(Changes("HEAD", "HEAD", [], full=True)):+ refs.extend((c.id, wf, text) for wf, text in c.ci)+ refs.extend(SYNC_REFS)+ return refs+++def verify_sync() -> int:+ bad = 0+ for root, (wf, _, _) in WINDOWS_LANES.items():+ if not windows_lane(root):+ bad += 1+ print(f"out of sync: rust-windows:{root}: cannot find the Windows lane command in .github/workflows/{wf}")+ for cid, wf, text in all_check_refs():+ path = ROOT / ".github/workflows" / wf+ body = path.read_text() if path.exists() else ""+ if text not in body:+ bad += 1+ print(f"out of sync: {cid}: .github/workflows/{wf} no longer contains: {text}")+ if bad:+ print("bin/ci --verify-sync: update bin/ci so it runs what CI runs (or put the text back in the workflow)")+ return 1+ print(f"bin/ci --verify-sync: {len(all_check_refs())} workflow commands match")+ return 0+++# ---------------------------------------------------------------- main+++def parse_args(argv: Sequence[str]) -> argparse.Namespace:+ p = argparse.ArgumentParser(prog="bin/ci", description=__doc__.split("\n\n")[0],+ formatter_class=argparse.RawDescriptionHelpFormatter, epilog="\n\n".join(__doc__.split("\n\n")[1:]))+ p.add_argument("--full", action="store_true", help="run every check, not only the affected ones")+ p.add_argument("--quick", action="store_true", help="only checks that build nothing (seconds)")+ p.add_argument("--fix", action="store_true", help="apply the safe fixers for failed checks, then re-check")+ p.add_argument("--list", action="store_true", help="print the plan and exit")+ p.add_argument("--only", metavar="IDS", help="comma-separated check ids or prefixes (see --list)")+ p.add_argument("--base", metavar="REF", help="compare with REF (default: merge base with origin/main)")+ p.add_argument("--jobs", "-j", type=int, help="checks to run at once (default: half the CPUs, 2 to 6)")+ p.add_argument("--no-install", action="store_true", help="never install node_modules a check needs; skip it instead")+ p.add_argument("--signoff", action="store_true", help=f"set the `{STATUS_CONTEXT}` commit status after a green run")+ p.add_argument("--install-hook", action="store_true", help="install the opt-in pre-push hook")+ p.add_argument("--uninstall-hook", action="store_true", help="remove the pre-push hook")+ p.add_argument("--force", action="store_true", help=argparse.SUPPRESS)+ p.add_argument("--hook", action="store_true", help=argparse.SUPPRESS)+ p.add_argument("--verify-sync", action="store_true", help="fail if a workflow no longer runs a command bin/ci mirrors")+ return p.parse_args(argv)+++def main(argv: Sequence[str]) -> int:+ args = parse_args(argv)+ os.chdir(ROOT)+ if args.verify_sync:+ return verify_sync()+ if args.install_hook:+ return install_hook(args.force)+ if args.uninstall_hook:+ return uninstall_hook()+ if args.hook:+ stdin_text = sys.stdin.read() if not sys.stdin.isatty() else ""+ if hook_refs_are_deletions(stdin_text):+ return 0+ print("bin/ci: checking what this push changes (skip once: CUA_SKIP_LOCAL_CI=1 git push)", flush=True)++ mode = "full" if args.full else ("quick" if args.quick else "affected")+ if args.signoff and not args.full and not args.quick and not args.only:+ last = state_dir() / "last-run.json"+ if last.exists():+ state = json.loads(last.read_text())+ if state.get("head") == git("rev-parse", "HEAD").strip() and state.get("clean") and worktree_clean() and state.get("mode") in ("affected", "full"):+ print(f"bin/ci: reusing the {state['mode']} run of {state['head'][:10]}")+ return signoff(state)++ base, base_desc = find_base(args.base)+ changes = Changes(base, base_desc, changed_files(base), full=args.full)+ if not changes.files and not args.full:+ print(f"bin/ci: nothing changed against {base[:10]} ({base_desc})")+ return 0+ ctx = Context(args)+ started = time.monotonic()+ plan = build_plan(changes, ctx)+ scope = "every check" if args.full else f"{len(changes.files)} changed file(s) since {base[:10]} ({base_desc})"+ print(f"bin/ci: {mode} mode, {scope}: {len(plan)} check(s)")+ for n in ctx.notes:+ print(f" note: {n}")+ if args.list:+ for c in plan:+ extra = f" [{c.skip_reason}]" if c.skip_reason else (" (heavy)" if c.heavy else "")+ print(f" {c.id:<38} {c.title}{extra}")+ print(f" {'':<38} $ (cd {c.cwd} && {c.cmd[:160]}{'...' if len(c.cmd) > 160 else ''})")+ return 0+ if not plan:+ print("bin/ci: no local check covers these files")+ return 0++ logdir = state_dir() / "logs"+ for old in logdir.glob("*.log"):+ old.unlink()+ results = run_plan(plan, ctx, logdir)+ if args.fix and any(r.status == "FAIL" for r in results):+ results = apply_fixes(results, ctx, logdir)+ elapsed = time.monotonic() - started+ state = save_state(results, mode, elapsed)++ failed = [r for r in results if r.status == "FAIL"]+ counts = {s: sum(1 for r in results if r.status == s) for s in ("pass", "fixed", "FAIL", "skip", "warn")}+ summary = ", ".join(f"{n} {label}" for label, n in (("passed", counts["pass"]), ("fixed", counts["fixed"]), ("failed", counts["FAIL"]),+ ("skipped", counts["skip"]), ("warnings", counts["warn"])) if n)+ print(f"bin/ci: {summary} in {fmt_secs(elapsed)}")+ if failed:+ safe = [r for r in failed if r.check.safe_fix and fix_commands(r)]+ if safe and not args.fix:+ print(f"bin/ci: `bin/ci --fix` repairs {len(safe)} of the {len(failed)} failure(s): {', '.join(r.check.id for r in safe)}")+ if args.hook:+ print("bin/ci: push stopped. Fix the failures, or push anyway with CUA_SKIP_LOCAL_CI=1 git push")+ return 1+ if args.signoff:+ return signoff(state)+ return 0+++if __name__ == "__main__":+ try:+ sys.exit(main(sys.argv[1:]))+ except KeyboardInterrupt:+ sys.exit(130)diff --git a/docs/contributing/CI.md b/docs/contributing/CI.mdindex 122aae8bae..dff3610b6f 100644--- a/docs/contributing/CI.md+++ b/docs/contributing/CI.md@@ -3,6 +3,62 @@Every pull request must pass one required check, **CI: gate** (`ci-gate`). The gate waits for the otherpull request workflows and passes when they pass. This page explains which of those workflows it waits for.+## Run the checks locally first: `bin/ci`++`bin/ci` runs, on your machine, the CI checks that your change can break, and prints the command that fixes+each failure. Run it before you push. It catches in minutes what otherwise costs a full CI round:++- rustfmt, and clippy `-D warnings` on the crates you changed and the crates that depend on them;+- each workspace's Windows CI lane, cross-compiled with `cargo xwin` on the same packages (read from the+ workflow), which catches code that only Unix uses. A C dependency that cannot be cross-built on your+ machine is reported as a warning, and every crate that does not need it is still checked;+- lockfiles that no longer match their manifests, including the nested workspaces under `examples/` and `tests/`;+- stale generated files: the cua SDK and Cua Driver UniFFI bindings, the Electron and SwiftUI app bindings,+ the Spaces contract manifest and control fixtures, and the Cua Driver contract;+- stale generated reference docs, docs code blocks with no disposition, and image refs outside the catalog;+- Python isort, black and ruff; TypeScript typecheck, tests and the design lint for the web packages you touched;+- actionlint for workflows, SPDX headers, and the unit tests of the crates you changed.++```bash+bin/ci # the checks your change affects (compares with the merge base with origin/main)+bin/ci --quick # only checks that build nothing: seconds+bin/ci --fix # apply the safe fixers (formatters, lockfiles, generators), then check again+bin/ci --full # every check, whatever you changed+bin/ci --list # show the plan and the exact commands+bin/ci --only rust-fmt,lockfiles+```++The commands are the ones the workflows run. Each check names the workflow text it mirrors, and+`bin/ci --verify-sync` (run by **CI: Test Scripts**) fails when a workflow stops running it. When you change+how a workflow runs a check, change `bin/ci` in the same pull request.++A check whose tool is missing is reported as skipped, never as passed, with the install command. `bin/ci`+uses the pinned Rust toolchain (`rust-toolchain.toml`) through rustup even when another `cargo` comes first on+your `PATH`, and installs a web package's `node_modules` with its frozen lockfile when a check needs them+(`--no-install` skips those checks instead). Logs are kept under `.git/bin-ci/logs/`.++What it cannot catch: failures that only happen on a real Windows or Linux host (sockets, permissions,+installers), the end-to-end and image lanes, and flakes. CI still runs those.++### Pre-push hook (opt-in)++```bash+bin/ci --install-hook # runs bin/ci before every git push from this clone and its worktrees+bin/ci --uninstall-hook+CUA_SKIP_LOCAL_CI=1 git push # skip it once+```++The hook goes in the clone's `hooks/pre-push`. An existing pre-push hook is kept and still runs first.+If you set a global `core.hooksPath`, your global pre-push hook must chain to the repository hook; the+installer tells you whether it does. Prefer `CUA_SKIP_LOCAL_CI=1` to `git push --no-verify`, which also+skips every other pre-push hook. `CUA_LOCAL_CI_ARGS="--quick"` changes what the hook runs.++### Local sign-off (optional)++After a green run on a clean tree, `bin/ci --signoff` sets the `local-ci` commit status on `HEAD` through+`gh api`. If the commit is not on GitHub yet, push and run it again: it reuses the green run. It refuses to+sign off a run that failed or skipped a check. The status is informational; `ci-gate` does not require it.+## Path-scoped gateA workflow still starts on its own `paths:` filter. The gate then waits only for the workflows that belongdiff --git a/scripts/ci/prepush.sh b/scripts/ci/prepush.shindex 2cdf938865..1192a68fea 100755--- a/scripts/ci/prepush.sh+++ b/scripts/ci/prepush.sh@@ -1,137 +1,22 @@#!/usr/bin/env bash#-# Local pre-push checks: the cheap CI gates that fail pull requests for-# reasons you can find in seconds on your own machine (headers, lockfiles,-# generated files, formatting, clippy on the crates you changed, Windows-# cfg errors, python and web UI contract checks). Run it before every push; it only looks at files that differ-# from the merge base, so an untouched area costs nothing.+# Kept for old muscle memory and scripts: the local pre-push checks now live+# in bin/ci (see docs/contributing/CI.md). This maps the old flags onto it.#-# scripts/ci/prepush.sh # fast checks, then clippy for changed crates-# scripts/ci/prepush.sh --fast # skip clippy and Windows checks (seconds)-# scripts/ci/prepush.sh --bindings # also regenerate and compare the UniFFI bindings-# scripts/ci/prepush.sh --base REF # compare with REF (default: merge base with origin/main)-#-# Heavy cargo commands honour CUA_BUILD_WRAPPER, for example-# CUA_BUILD_WRAPPER=/path/to/your-build-wrapper.sh scripts/ci/prepush.sh-# A check whose tool is missing is reported as SKIPPED, never as passed.-set -uo pipefail--fast=0 bindings=0 base=""+# scripts/ci/prepush.sh -> bin/ci+# scripts/ci/prepush.sh --fast -> bin/ci --quick+# scripts/ci/prepush.sh --bindings -> bin/ci (bindings run when your change affects them)+# scripts/ci/prepush.sh --base REF -> bin/ci --base REF+set -euo pipefail+args=()while [ $# -gt 0 ]; docase "$1" in- --fast) fast=1 ;;- --bindings) bindings=1 ;;- --base) base="$2"; shift ;;- -h|--help) sed -n '4,18p' "$0"; exit 0 ;;- *) echo "unknown option: $1" >&2; exit 2 ;;+ --fast) args+=(--quick) ;;+ --bindings) ;;+ --base) args+=(--base "$2"); shift ;;+ -h|--help) sed -n '3,9p' "$0"; exit 0 ;;+ *) args+=("$1") ;;esacshiftdone--root="$(git rev-parse --show-toplevel)" || exit 2-cd "$root" || exit 2-if [ -z "$base" ]; then- for r in origin/main main; do- git rev-parse -q --verify "$r" >/dev/null && { base="$(git merge-base HEAD "$r")"; break; }- done-fi-[ -n "$base" ] || { echo "cannot find a base commit; pass --base REF" >&2; exit 2; }-# Committed changes plus anything still uncommitted or untracked.-changed="$( { git diff --name-only "$base"...HEAD; git diff --name-only HEAD; git ls-files --others --exclude-standard; } 2>/dev/null | sort -u )"-[ -n "$changed" ] || { echo "no changes against $base"; exit 0; }--wrap=${CUA_BUILD_WRAPPER:-}-failed=() skipped=() passed=()-touches() { printf '%s\n' "$changed" | grep -Eq "$1"; }-step() { # name, command...- local name="$1"; shift- echo; echo "== $name"- if "$@"; then passed+=("$name"); else failed+=("$name"); fi-}-skip() { echo; echo "== $1: SKIPPED ($2)"; skipped+=("$1 ($2)"); }--# Headers, lockfiles, workflows, generated image constants.-if touches '\.(rs|py|ts|tsx|js|mjs|swift|sh)$|^scripts/spdx-headers\.py$'; then- step "SPDX headers (FSL)" python3 scripts/spdx-headers.py --fsl --check-fi-if touches '(^|/)(Cargo\.(toml|lock)|package(-lock)?\.json|pnpm-lock\.yaml|uv\.lock|Package\.resolved)$'; then- if command -v npm >/dev/null; then step "lockfiles match manifests" scripts/ci/check-lockfiles.sh- else skip "lockfiles match manifests" "npm not installed"; fi-fi-if touches '^\.github/(workflows|actions)/'; then- if command -v actionlint >/dev/null; then step "actionlint" actionlint -shellcheck= -pyflakes=- else skip "actionlint" "install with: brew install actionlint"; fi-fi-if touches '^(scripts/images/|libs/images/)'; then- step "image refs and constants" bash -c 'python3 -m unittest discover -s scripts/images/tests && python3 scripts/images/gen-image-constants.py --check && python3 scripts/images/record-image-sizes.py --check'-fi-if touches '^docs/'; then- if [ -x docs/node_modules/tsx/dist/cli.mjs ] || [ -e docs/node_modules/tsx/dist/cli.mjs ]; then- step "generated docs are current" bash -c 'for l in cua-driver; do node docs/node_modules/tsx/dist/cli.mjs scripts/docs-generators/runner.ts --library "$l" --check || exit 1; done'- else skip "generated docs are current" "run: pnpm --dir docs install --frozen-lockfile"; fi-fi--# Rust: formatting, then clippy (and Windows cfg) for the crates that changed.-workspaces=(libs/cua libs/cua-spacesd libs/cua-driver/rust)-crate_of() { # file -> "workspace<TAB>package"- local d; d="$(dirname "$1")"- while [ "$d" != "." ] && [ "$d" != "/" ]; do- if [ -f "$d/Cargo.toml" ] && grep -q '^\[package\]' "$d/Cargo.toml"; then- printf '%s\t%s\n' "$d" "$(awk '/^\[package\]/{p=1;next} /^\[/{p=0} p && /^name *=/{gsub(/^name *= *"|"[ ]*$/,""); print; exit}' "$d/Cargo.toml")"- return- fi- d="$(dirname "$d")"- done-}-for ws in "${workspaces[@]}"; do- files="$(printf '%s\n' "$changed" | grep -E "^$ws/.*\.rs$|^$ws/.*Cargo\.toml$" || true)"- [ -n "$files" ] || continue- pkgs=()- while IFS= read -r f; do- [ -e "$f" ] || continue- line="$(crate_of "$f")"; [ -n "$line" ] || continue- dir="${line%%$'\t'*}"; pkg="${line##*$'\t'}"- # only members of this workspace (skip nested standalone workspaces)- [ -n "$pkg" ] && (cd "$ws" && cargo metadata --no-deps --format-version 1 2>/dev/null | grep -q "\"name\":\"$pkg\"") && pkgs+=("$pkg")- done <<<"$files"- [ ${#pkgs[@]} -gt 0 ] || continue- uniq_pkgs="$(printf '%s\n' "${pkgs[@]}" | sort -u)"- pkgs=(); pflags=()- for p in $uniq_pkgs; do pkgs+=("$p"); pflags+=(-p "$p"); done- step "cargo fmt ($ws: ${pkgs[*]})" bash -c "cd $ws && cargo fmt ${pflags[*]} -- --check"- if [ $fast -eq 0 ]; then- step "clippy ($ws: ${pkgs[*]})" bash -c "cd $ws && $wrap cargo clippy --locked ${pflags[*]} --all-targets -- -D warnings"- if cargo xwin --version >/dev/null 2>&1; then- step "Windows clippy ($ws)" bash -c "cd $ws && $wrap cargo xwin clippy --locked --target x86_64-pc-windows-msvc ${pflags[*]} --all-targets -- -D warnings"- else- skip "Windows clippy ($ws)" "install cargo-xwin: cargo install cargo-xwin && rustup target add x86_64-pc-windows-msvc"- fi- fi-done--# Python: the same three tools as CI: Lint Python (black, not ruff format).-if touches '^(libs/python/|tests/|pyproject\.toml$)'; then- if command -v uv >/dev/null; then- step "python isort, black, ruff" bash -c 'uv run isort --check-only libs/python tests && uv run black --check libs/python tests && uv run ruff check libs/python tests'- else skip "python isort, black, ruff" "install uv"; fi-fi--# Cua Spaces web UI, bridge contract (Electron/browser binding drift) and Electron shell.-if touches '^(apps/cua-spaces-web/|apps/cua-spaces-desktop/|apps/cua-spaces/core-wasm/|libs/cua/crates/cua-spaces-app-core/)'; then- if [ -d apps/cua-spaces-web/node_modules ] && [ -d apps/cua-spaces-desktop/node_modules ]; then- step "web UI typecheck, tests and bridge contract" bash -c 'cd apps/cua-spaces-web && pnpm check'- step "Electron shell typecheck and router contract" bash -c 'cd apps/cua-spaces-desktop && pnpm test'- else skip "web UI and Electron contract" "run pnpm install in apps/cua-spaces-web (--ignore-workspace) and apps/cua-spaces-desktop"; fi-fi--if [ $bindings -eq 1 ]; then- step "UniFFI bindings are current" bash -c "$wrap node libs/cua/scripts/generate-uniffi-bindings.mjs --check"-elif touches '^libs/cua/(crates/cua-sdk|crates/cua-spaces|uniffi|python|typescript|swift|kotlin)'; then- echo; echo "note: you changed SDK or binding sources; run with --bindings to compare the generated files"-fi--echo; echo "---- summary"-for x in ${passed[@]+"${passed[@]}"}; do echo "pass $x"; done-for x in ${skipped[@]+"${skipped[@]}"}; do echo "SKIPPED $x"; done-for x in ${failed[@]+"${failed[@]}"}; do echo "FAIL $x"; done-[ ${#failed[@]} -eq 0 ]+exec "$(git rev-parse --show-toplevel)/bin/ci" ${args[@]+"${args[@]}"}diff --git a/scripts/ci/tests/test_bin_ci.py b/scripts/ci/tests/test_bin_ci.pynew file mode 100644index 0000000000..3ba2d13ca0--- /dev/null+++ b/scripts/ci/tests/test_bin_ci.py@@ -0,0 +1,227 @@+"""Tests for bin/ci: which checks a change selects, fix parsing, the hook, and workflow sync."""++from __future__ import annotations++import importlib.machinery+import importlib.util+import os+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++REPO = Path(__file__).resolve().parents[3]+++def load():+ loader = importlib.machinery.SourceFileLoader("bin_ci", str(REPO / "bin/ci"))+ spec = importlib.util.spec_from_loader("bin_ci", loader)+ mod = importlib.util.module_from_spec(spec)+ sys.modules["bin_ci"] = mod # dataclasses look the module up+ loader.exec_module(mod)+ return mod+++ci = load()+++class FakeContext:+ """Routes without cargo or the docs runner."""++ def __init__(self, generators=(), crate_hits=()):+ self.notes = []+ self.rust_env = {}+ self._generators = list(generators)+ self._crate_hits = set(crate_hits)++ def docs_generators(self, changes):+ return self._generators++ def crate_trigger(self, changes, ws_root, roots, path_rx):+ return bool(changes.touches(path_rx)) or bool(self._crate_hits & set(roots))+++def ids(files, **kw):+ changes = ci.Changes("HEAD", "HEAD", files)+ return {c.id for c in ci.static_checks(changes, FakeContext(**kw))}+++class Routing(unittest.TestCase):+ def test_workflow_change_runs_actionlint_and_sync(self):+ got = ids([".github/workflows/ci-gate.yml"])+ self.assertIn("actionlint", got)+ self.assertIn("bin-ci-tests", got)++ def test_nested_cargo_manifest_runs_the_cargo_lockfile_check(self):+ # PR 6358: a libs/cua crate's manifest changed and nested workspaces'+ # Cargo.lock files (examples/, tests/e2e/) drifted.+ changes = ci.Changes("HEAD", "HEAD", ["libs/cua/crates/cua-vmm/Cargo.toml"])+ lock = [c for c in ci.static_checks(changes, FakeContext()) if c.id == "lockfiles"]+ self.assertEqual(len(lock), 1)+ self.assertIn("--kind cargo", lock[0].cmd)+ self.assertNotIn("--kind uv", lock[0].cmd)++ def test_sdk_source_runs_the_bindings_checks(self):+ got = ids(["libs/cua/crates/cua-sdk/src/lib.rs"])+ self.assertIn("sdk-bindings", got)+ self.assertIn("spaces-app-bindings", got)++ def test_spaces_crate_runs_the_contract_check(self):+ self.assertIn("spaces-contract", ids(["libs/cua/crates/cua-spaces/src/spaces.rs"], crate_hits={"cua-spaces"}))+ self.assertNotIn("spaces-contract", ids(["libs/cua/crates/cua-image/src/manifest.rs"]))++ def test_driver_contract_crate_runs_contract_and_bindings(self):+ got = ids(["libs/cua-driver/rust/crates/cua-driver-contract/src/outputs.rs"],+ crate_hits={"cua-driver-contract", "cua-driver-sdk"})+ self.assertIn("driver-contract", got)+ self.assertIn("driver-bindings", got)++ def test_docs_page_runs_code_block_and_image_ref_checks(self):+ got = ids(["docs/content/docs/spaces/guides/connect-your-agent.mdx"])+ self.assertIn("docs-code-blocks", got)+ self.assertIn("docs-image-refs", got)++ def test_docs_reference_rows_come_from_the_router(self):+ got = ids(["libs/cua/crates/cua-spaces/src/x.rs"], generators=["spaces", "cua-sdk"])+ self.assertIn("docs-ref:spaces", got)+ self.assertIn("docs-ref:cua-sdk", got)++ def test_image_refs_gate_runs_for_any_change(self):+ self.assertIn("image-refs", ids(["README.md"]))++ def test_python_checks_mirror_ci_scope(self):+ # PR 6318: a uv.lock change made black fail a file the PR never touched.+ for files, want in ((["tests/e2e/cua-sdk/python/conftest.py"], 1), (["uv.lock"], 1), (["libs/cua-s1/x.py"], 0)):+ checks = ci.python_checks(ci.Changes("HEAD", "HEAD", files))+ self.assertEqual(len(checks), want, files)+ self.assertIn("black --check libs/python tests", ci.python_checks(ci.Changes("HEAD", "HEAD", ["uv.lock"]))[0].cmd)++ def test_web_ui_change_runs_typecheck_design_lint_and_tests(self):+ changes = ci.Changes("HEAD", "HEAD", ["apps/cua-spaces-web/src/main.tsx"])+ got = {c.id for c in ci.web_checks(changes)}+ self.assertTrue({"web:apps/cua-spaces-web:typecheck", "web:apps/cua-spaces-web:design-lint",+ "web:apps/cua-spaces-web:test"} <= got)+ self.assertFalse(any(i.startswith("web:apps/cua-spaces-desktop") for i in got))++ def test_full_mode_selects_everything(self):+ changes = ci.Changes("HEAD", "HEAD", [], full=True)+ got = {c.id for c in ci.static_checks(changes, FakeContext(generators=["spaces"]))}+ for want in ("lockfiles", "actionlint", "sdk-bindings", "spaces-contract", "driver-contract", "docs-ref:spaces"):+ self.assertIn(want, got)+++class Graph(unittest.TestCase):+ ws = ci.Workspace("libs/x", {"a": "a", "b": "b", "c": "c", "d": "d"},+ {"a": [], "b": ["a"], "c": ["b"], "d": []})++ def test_dependents_are_transitive(self):+ self.assertEqual(ci.dependents(self.ws, ["a"]), ["b", "c"])+ self.assertEqual(ci.dependents(self.ws, ["d"]), [])++ def test_deps_closure(self):+ self.assertEqual(ci.deps_closure(self.ws, ["c"]), {"a", "b", "c"})++ def test_package_of_finds_the_owning_crate(self):+ self.assertEqual(ci.package_of("libs/cua/crates/cua-sdk/src/lib.rs"), ("libs/cua/crates/cua-sdk", "cua-sdk"))+ self.assertIsNone(ci.package_of("README.md"))+++class Fixes(unittest.TestCase):+ def test_lockfile_fix_commands_come_from_the_checker_output(self):+ check = ci.Check("lockfiles", "t", "true", fix_from_output=r"^FAIL .*?; fix: (.+)$")+ with tempfile.NamedTemporaryFile("w", suffix=".log", delete=False) as fh:+ fh.write("ok libs/cua/Cargo.lock\n"+ "FAIL examples/streaming/rust/Cargo.lock drifted; fix: (cd examples/streaming/rust && cargo update --workspace)\n"+ "::error::lockfile drift: (cd examples/streaming/rust && cargo update --workspace)\n")+ try:+ r = ci.Result(check, "FAIL", log=Path(fh.name))+ self.assertEqual(ci.fix_commands(r), ["(cd examples/streaming/rust && cargo update --workspace)"])+ finally:+ os.unlink(fh.name)++ def test_excerpt_prefers_error_lines(self):+ with tempfile.NamedTemporaryFile("w", suffix=".log", delete=False) as fh:+ fh.write(" Compiling foo v1\nnoise\nDiff in /x/src/a.rs:776:\nmore noise\n")+ try:+ self.assertEqual(ci.excerpt(Path(fh.name)), ["Diff in /x/src/a.rs:776:"])+ finally:+ os.unlink(fh.name)+++class Windows(unittest.TestCase):+ def test_a_rustc_error_fails(self):+ # PR 6355: Unix-only code left an unused import on Windows.+ log = "error: unused import: `AsyncBufReadExt`\n --> crates\\cua-spaces\\src\\tree_hub.rs:32:17\nerror: could not compile `cua-spaces` (lib)\n"+ self.assertEqual(ci.classify_xwin(log, 101)[0], "FAIL")++ def test_a_c_dependency_that_cannot_cross_build_only_warns(self):+ log = "error: failed to run custom build command for `audiopus_sys v0.2.2`\nwarning: build failed, waiting for other jobs to finish...\n"+ status, detail = ci.classify_xwin(log, 101)+ self.assertEqual(status, "warn")+ self.assertIn("audiopus_sys", detail)++ def test_clean_build_passes(self):+ self.assertEqual(ci.classify_xwin("Finished", 0)[0], "pass")+++class Hook(unittest.TestCase):+ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.repo = Path(self.tmp.name)+ subprocess.run(["git", "init", "-q", str(self.repo)], check=True)+ self._root = ci.ROOT+ ci.ROOT = self.repo+ self.hooks = self.repo / ".git/hooks"++ def tearDown(self):+ ci.ROOT = self._root+ self.tmp.cleanup()++ def test_install_keeps_and_chains_an_existing_hook_then_uninstall_restores_it(self):+ self.hooks.mkdir(exist_ok=True)+ (self.hooks / "pre-push").write_text("#!/bin/sh\necho mine\n")+ self.assertEqual(ci.install_hook(force=False), 0)+ hook = (self.hooks / "pre-push").read_text()+ self.assertIn(ci.HOOK_MARKER, hook)+ self.assertIn(ci.CHAINED_HOOK, hook)+ self.assertEqual((self.hooks / ci.CHAINED_HOOK).read_text(), "#!/bin/sh\necho mine\n")+ self.assertEqual(ci.install_hook(force=False), 0) # idempotent+ self.assertEqual(ci.uninstall_hook(), 0)+ self.assertEqual((self.hooks / "pre-push").read_text(), "#!/bin/sh\necho mine\n")++ def test_hook_runs_the_chained_hook_first_and_stops_on_its_failure(self):+ self.hooks.mkdir(exist_ok=True)+ (self.hooks / "pre-push").write_text("#!/bin/sh\necho guard-ran; exit 3\n")+ (self.hooks / "pre-push").chmod(0o755)+ ci.install_hook(force=False)+ out = subprocess.run([str(self.hooks / "pre-push"), "origin", "url"], input="refs/heads/x 1 refs/heads/x 0\n",+ capture_output=True, text=True, cwd=self.repo)+ self.assertEqual(out.returncode, 3)+ self.assertIn("guard-ran", out.stdout)++ def test_skip_variable(self):+ ci.install_hook(force=False)+ out = subprocess.run([str(self.hooks / "pre-push"), "origin", "url"], input="", capture_output=True, text=True,+ cwd=self.repo, env={**os.environ, "CUA_SKIP_LOCAL_CI": "1"})+ self.assertEqual(out.returncode, 0)+ self.assertIn("skipped", out.stderr)++ def test_deleting_a_branch_checks_nothing(self):+ zero = "0" * 40+ self.assertTrue(ci.hook_refs_are_deletions(f"(delete) {zero} refs/heads/x abc\n"))+ self.assertFalse(ci.hook_refs_are_deletions(f"refs/heads/x abc refs/heads/x {zero}\n"))+++class Sync(unittest.TestCase):+ def test_every_mirrored_command_is_still_in_its_workflow(self):+ missing = []+ for cid, wf, text in ci.all_check_refs():+ path = REPO / ".github/workflows" / wf+ if not path.exists() or text not in path.read_text():+ missing.append(f"{cid}: {wf}: {text}")+ self.assertEqual(missing, [])+++if __name__ == "__main__":+ unittest.main()