Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

ci: build the SwiftUI app library once and run the Swift checks in parallel (#5824)

Francesco Bonaccic8013fb5ee33
diff --git a/.github/actions/spaces-rust-slice/action.yml b/.github/actions/spaces-rust-slice/action.yml
index 36243da054..a09f2bea0c 100644
--- a/.github/actions/spaces-rust-slice/action.yml
+++ b/.github/actions/spaces-rust-slice/action.yml
@@ -8,20 +8,42 @@ description: >-
that main already built restores the slice instead of compiling for 33 to
60 minutes. The key is the git blob list of the inputs of the build: the
Rust workspace without documentation and the non-Rust language packages,
- the cua-spacesd pin, the target and the toolchain. A miss builds (with the
- Rust dependency cache) and saves. Signing and verification of the app stay
- in cd-cua-spaces.yml and run on whatever this action leaves in place. Run
- after actions/checkout.
+ every file outside it that the crates of the build read at compile time
+ (found by .github/scripts/spaces_rust_slice_inputs.py from their
+ `include_str!` calls and build scripts: the image catalog, the shared
+ keybindings, the cua-spacesd pin, the skills, the cargo config), the target
+ and the toolchain. A miss builds (with the Rust dependency cache) and
+ saves. Signing and verification of the app stay in cd-cua-spaces.yml and
+ run on whatever this action leaves in place. Run after actions/checkout.
+
+ With `build: "false"` the action only restores (ci-cua-spaces-macos.yml:
+ a pull request that does not change the Rust inputs takes main's compiled
+ library and skips the 30 to 60 minute build; one that does falls back to
+ its own build). Same key either way, so there is one definition of it.
inputs:
triple:
description: "Rust target triple (aarch64-apple-darwin or x86_64-apple-darwin)."
required: true
+ build:
+ description: >-
+ "true" (default): build on a miss and save, as the producer and the
+ release do. "false": restore only; on a miss nothing is built or saved
+ and the caller builds for itself.
+ required: false
+ default: "true"
+ verify-key:
+ description: >-
+ "true": after a build, fail if the compiler or a build script read a
+ tracked file the cache key does not hash (the producer does this; the
+ release does not, so a stale key can never block a release).
+ required: false
+ default: "false"
outputs:
cache-hit:
description: "true when the slice came from the cache."
- value: ${{ steps.cache.outputs.cache-hit }}
+ value: ${{ steps.cache.outputs.cache-hit || steps.restore.outputs.cache-hit }}
key:
description: "The cache key."
value: ${{ steps.key.outputs.key }}
@@ -36,37 +58,45 @@ runs:
TRIPLE: ${{ inputs.triple }}
run: |
set -euo pipefail
- # Hash the staged blobs (mode, object id, path) of what the build
- # reads. Documentation and the Kotlin, Python and TypeScript packages
+ # Hash the staged blobs (mode, object id, path) of everything the
+ # build reads: libs/cua and the files outside it that its crates
+ # embed. Documentation and the Kotlin, Python and TypeScript packages
# are not inputs: a changelog or README edit must not cost a rebuild.
- inputs_hash="$( { git ls-files -s -- libs/cua libs/cua-spacesd/VERSION \
- ':(exclude)libs/cua/kotlin' ':(exclude)libs/cua/python' \
- ':(exclude)libs/cua/typescript' ':(exclude)libs/cua/skills' \
- ':(exclude,glob)**/*.md'; } | git hash-object --stdin)"
+ inputs_hash="$(python3 .github/scripts/spaces_rust_slice_inputs.py key)"
repo="${CUA_RELEASE_REPOSITORY//\//-}"
- echo "key=swiftui-rust-v2-${TRIPLE}-rust1.97.1-${repo}-${inputs_hash}" >> "$GITHUB_OUTPUT"
+ echo "key=swiftui-rust-v3-${TRIPLE}-rust1.97.1-${repo}-${inputs_hash}" >> "$GITHUB_OUTPUT"
- name: Restore the compiled slice
id: cache
+ if: inputs.build == 'true'
uses: actions/cache@v4
with:
path: |
libs/cua/target/${{ inputs.triple }}/release/libcua_spaces_ffi.dylib
libs/cua/target/${{ inputs.triple }}/release/cua-spaces-cli
key: ${{ steps.key.outputs.key }}
+ - name: Restore the compiled slice (restore only)
+ id: restore
+ if: inputs.build != 'true'
+ uses: actions/cache/restore@v4
+ with:
+ path: |
+ libs/cua/target/${{ inputs.triple }}/release/libcua_spaces_ffi.dylib
+ libs/cua/target/${{ inputs.triple }}/release/cua-spaces-cli
+ key: ${{ steps.key.outputs.key }}
- name: Report the cache
shell: bash
env:
- HIT: ${{ steps.cache.outputs.cache-hit || 'false' }}
+ HIT: ${{ steps.cache.outputs.cache-hit || steps.restore.outputs.cache-hit || 'false' }}
KEY: ${{ steps.key.outputs.key }}
run: echo "::notice::SwiftUI Rust slice cache hit = ${HIT} (${KEY})"
- - if: steps.cache.outputs.cache-hit != 'true'
+ - if: inputs.build == 'true' && steps.cache.outputs.cache-hit != 'true'
uses: dtolnay/[email protected]
with:
targets: ${{ inputs.triple }}
- - if: steps.cache.outputs.cache-hit != 'true'
+ - if: inputs.build == 'true' && steps.cache.outputs.cache-hit != 'true'
name: Install protoc
uses: ./.github/actions/setup-protoc
- - if: steps.cache.outputs.cache-hit != 'true'
+ - if: inputs.build == 'true' && steps.cache.outputs.cache-hit != 'true'
uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2
with:
workspaces: libs/cua -> target
@@ -76,7 +106,7 @@ runs:
# within the hour. Only the default branch saves it.
save-if: ${{ github.ref == 'refs/heads/main' }}
cache-on-failure: true
- - if: steps.cache.outputs.cache-hit != 'true'
+ - if: inputs.build == 'true' && steps.cache.outputs.cache-hit != 'true'
name: Build the slice
shell: bash
working-directory: libs/cua
@@ -87,7 +117,15 @@ runs:
CUA_DEFAULT_ENV: ""
TRIPLE: ${{ inputs.triple }}
run: cargo build --locked --release -p cua-spaces-ffi -p cua-spaces-cli --target "$TRIPLE"
+ # Ground truth for the key: what rustc and the build scripts really read.
+ - if: inputs.build == 'true' && inputs.verify-key == 'true' && steps.cache.outputs.cache-hit != 'true'
+ name: The cache key covers every file the build read
+ shell: bash
+ env:
+ TRIPLE: ${{ inputs.triple }}
+ run: python3 .github/scripts/spaces_rust_slice_inputs.py verify "libs/cua/target/${TRIPLE}/release"
- name: Check the slice
+ if: inputs.build == 'true' || steps.restore.outputs.cache-hit == 'true'
shell: bash
env:
TRIPLE: ${{ inputs.triple }}
diff --git a/.github/scripts/spaces_rust_slice_inputs.py b/.github/scripts/spaces_rust_slice_inputs.py
new file mode 100644
index 0000000000..c74018411c
--- /dev/null
+++ b/.github/scripts/spaces_rust_slice_inputs.py
@@ -0,0 +1,574 @@
+#!/usr/bin/env python3
+"""What the SwiftUI app's compiled Rust slice is a function of.
+
+`.github/actions/spaces-rust-slice` keys the compiled library (libcua_spaces_ffi
+and the cua-spaces-cli binary) by a hash of its inputs. A restored library is
+only correct if that hash covers every file the compiler and the build scripts
+read. `libs/cua` is hashed whole, so the question is which files *outside* it
+(or inside it, but left out of the hash on purpose) the build reads. This
+script answers it from the code instead of from a list someone has to keep:
+
+ closure the workspace crates `cua-spaces-ffi` and `cua-spaces-cli` depend on
+ (normal and build dependencies, read from the Cargo.toml files;
+ dev-dependencies are not built)
+ reads in those crates' `src/` and `build.rs`: `include_str!`,
+ `include_bytes!`, `include!` and `include_dir!` with a literal or
+ `concat!(env!("CARGO_MANIFEST_DIR"), ...)` path, `#[path = "..."]`,
+ and every `"../..."` literal in a build script (they copy files
+ from elsewhere in the repository into OUT_DIR). Code under
+ `#[cfg(test)]` and `#[test]` is not part of the library and is
+ skipped. Anything it cannot classify counts as a read.
+ key sha256 of the `git ls-files -s` listing of libs/cua (minus the
+ language packages and Markdown, which the build does not read)
+ plus the reads that fall outside that, plus the cargo configuration
+ files Cargo merges into a build under libs/cua.
+
+Usage:
+ spaces_rust_slice_inputs.py key print the hash (the action's key)
+ spaces_rust_slice_inputs.py inputs print the extra input paths
+ spaces_rust_slice_inputs.py closure print the crates in the closure
+ spaces_rust_slice_inputs.py verify DIR DIR is a finished build's target
+ directory (target/<triple>): fail
+ if the compiler or a build script
+ read a tracked file the key misses
+
+`verify` is the ground truth: it reads the dep-info files rustc wrote and the
+`rerun-if-changed` lines of the build scripts, so a read this scanner cannot
+see (a new macro, a proc macro that opens a file) still fails the producer.
+"""
+
+from __future__ import annotations
+
+import hashlib
+import os
+import re
+import subprocess
+import sys
+from pathlib import Path
+
+try: # Python 3.11+
+ import tomllib as _toml
+
+ def _load_toml(path: Path) -> dict:
+ with open(path, "rb") as handle:
+ return _toml.load(handle)
+
+except ModuleNotFoundError: # pragma: no cover - depends on the interpreter
+ try:
+ import toml as _toml_pkg
+
+ def _load_toml(path: Path) -> dict:
+ return _toml_pkg.load(str(path))
+
+ except ModuleNotFoundError:
+ _load_toml = None # type: ignore[assignment]
+
+ROOTS = ("cua-spaces-ffi", "cua-spaces-cli")
+WORKSPACE = "libs/cua"
+# What the hash leaves out of libs/cua: the Kotlin, Python and TypeScript
+# packages and Markdown (see `_covered_by_base`). Anything the build reads from
+# there is added back through `extra_inputs` (libs/cua/skills is, by the
+# cua-agent-setup build script, which embeds its Markdown).
+HASHED_OUT = ("kotlin", "python", "typescript")
+# Cargo reads `.cargo/config.toml` from the working directory (libs/cua) up to
+# the root: rustflags and [env] of the build. The one inside libs/cua is
+# already hashed with the tree.
+CARGO_CONFIG_DIRS = (".cargo", "libs/.cargo")
+KEY_VERSION = "v1"
+
+
+# ----------------------------------------------------------------------------
+# Repository helpers
+
+
+def repo_root() -> Path:
+ out = subprocess.run(
+ ["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True, check=True
+ )
+ return Path(out.stdout.strip()).resolve()
+
+
+def _rel(root: Path, path: Path) -> str:
+ return os.path.relpath(os.path.normpath(path), root).replace(os.sep, "/")
+
+
+# ----------------------------------------------------------------------------
+# The workspace closure
+
+
+def _workspace_members(root: Path) -> dict[str, Path]:
+ """Crate name -> directory, for every Cargo.toml under libs/cua."""
+ found: dict[str, Path] = {}
+ base = root / WORKSPACE
+ for dirpath, dirnames, filenames in os.walk(base):
+ dirnames[:] = [d for d in dirnames if d not in ("target", "node_modules", ".git")]
+ if "Cargo.toml" in filenames and Path(dirpath) != base:
+ manifest = Path(dirpath) / "Cargo.toml"
+ if _load_toml is None:
+ match = re.search(r'(?m)^name\s*=\s*"([^"]+)"', manifest.read_text())
+ name = match.group(1) if match else Path(dirpath).name
+ else:
+ name = _load_toml(manifest).get("package", {}).get("name", Path(dirpath).name)
+ found[name] = Path(dirpath)
+ return found
+
+
+def _dependency_names(manifest: dict) -> list[tuple[str, dict]]:
+ """(key, spec) of every normal and build dependency, target tables included."""
+ tables = []
+ for kind in ("dependencies", "build-dependencies"):
+ tables.append(manifest.get(kind, {}))
+ for target in manifest.get("target", {}).values():
+ tables.append(target.get(kind, {}))
+ out = []
+ for table in tables:
+ for key, spec in table.items():
+ out.append((key, spec if isinstance(spec, dict) else {}))
+ return out
+
+
+def closure(root: Path) -> dict[str, Path]:
+ """The workspace crates the slice's two packages build (name -> directory).
+
+ Without a TOML parser (an old interpreter) every crate counts: a larger
+ closure only makes the key stricter."""
+ members = _workspace_members(root)
+ if _load_toml is None:
+ return members
+ workspace = _load_toml(root / WORKSPACE / "Cargo.toml")
+ shared = workspace.get("workspace", {}).get("dependencies", {})
+ by_dir = {path.resolve(): name for name, path in members.items()}
+ seen: dict[str, Path] = {}
+ stack = [name for name in ROOTS if name in members]
+ while stack:
+ name = stack.pop()
+ if name in seen:
+ continue
+ crate_dir = members[name]
+ seen[name] = crate_dir
+ manifest = _load_toml(crate_dir / "Cargo.toml")
+ for key, spec in _dependency_names(manifest):
+ real = spec.get("package", key)
+ path = spec.get("path")
+ if spec.get("workspace"):
+ shared_spec = shared.get(key, {})
+ shared_spec = shared_spec if isinstance(shared_spec, dict) else {}
+ real = shared_spec.get("package", real)
+ path = shared_spec.get("path")
+ base = root / WORKSPACE
+ else:
+ base = crate_dir
+ if path:
+ target = (base / path).resolve()
+ real = by_dir.get(target, real)
+ if real in members:
+ stack.append(real)
+ return seen
+
+
+# ----------------------------------------------------------------------------
+# Rust source scanning
+
+
+def _mask(src: str) -> str:
+ """`src` with comments and the inside of string and char literals blanked
+ (same length, newlines kept), so braces and attributes can be found safely."""
+ out = list(src)
+ n = len(src)
+ i = 0
+
+ def blank(a: int, b: int) -> None:
+ for k in range(a, b):
+ if out[k] != "\n":
+ out[k] = " "
+
+ while i < n:
+ two = src[i : i + 2]
+ c = src[i]
+ if two == "//":
+ j = src.find("\n", i)
+ j = n if j < 0 else j
+ blank(i, j)
+ i = j
+ elif two == "/*":
+ depth, j = 1, i + 2
+ while j < n and depth:
+ if src.startswith("/*", j):
+ depth += 1
+ j += 2
+ elif src.startswith("*/", j):
+ depth -= 1
+ j += 2
+ else:
+ j += 1
+ blank(i, j)
+ i = j
+ elif c in "rb" and (i == 0 or not (src[i - 1].isalnum() or src[i - 1] == "_")) and re.match(
+ r'(?:br|r)(#*)"', src[i : i + 40]
+ ):
+ m = re.match(r'(?:br|r)(#*)"', src[i : i + 40])
+ assert m
+ end = '"' + m.group(1)
+ start = i + m.end()
+ j = src.find(end, start)
+ j = n if j < 0 else j + len(end)
+ blank(start, j - len(end))
+ i = j
+ elif c == '"':
+ j = i + 1
+ while j < n and src[j] != '"':
+ j += 2 if src[j] == "\\" else 1
+ blank(i + 1, j)
+ i = j + 1
+ elif c == "'":
+ m = re.match(r"'(?:\\(?:u\{[0-9a-fA-F_]+\}|x[0-9a-fA-F]{2}|.)|[^\\'\n])'", src[i : i + 14])
+ if m:
+ blank(i + 1, i + m.end() - 1)
+ i += m.end()
+ else:
+ i += 1 # a lifetime
+ else:
+ i += 1
+ return "".join(out)
+
+
+_TEST_ATTR = re.compile(r"#\s*\[\s*(?:cfg\s*\(\s*test\s*\)|test|tokio\s*::\s*test\b[^\]]*)\s*\]")
+_INNER_TEST = re.compile(r"#\s*!\s*\[\s*cfg\s*\(\s*test\s*\)\s*\]")
+
+
+def test_spans(src: str, masked: str | None = None) -> list[tuple[int, int]]:
+ """Character ranges of items that only exist under `cfg(test)`."""
+ masked = _mask(src) if masked is None else masked
+ if _INNER_TEST.search(masked):
+ return [(0, len(src))]
+ spans = []
+ n = len(masked)
+ for m in _TEST_ATTR.finditer(masked):
+ i = m.end()
+ # More attributes on the same item.
+ while True:
+ while i < n and masked[i].isspace():
+ i += 1
+ if masked.startswith("#", i):
+ j = masked.find("[", i)
+ depth = 0
+ while j < n:
+ depth += masked[j] == "["
+ depth -= masked[j] == "]"
+ j += 1
+ if depth == 0:
+ break
+ i = j
+ else:
+ break
+ paren = 0
+ j = i
+ end = None
+ while j < n:
+ ch = masked[j]
+ if ch in "([":
+ paren += 1
+ elif ch in ")]":
+ paren -= 1
+ elif ch == ";" and paren == 0:
+ end = j + 1
+ break
+ elif ch == "{" and paren == 0:
+ depth = 0
+ while j < n:
+ depth += masked[j] == "{"
+ depth -= masked[j] == "}"
+ j += 1
+ if depth == 0:
+ break
+ end = j
+ break
+ j += 1
+ if end is not None:
+ spans.append((m.start(), end))
+ return spans
+
+
+_MAY_READ = re.compile(r"\binclude(?:_str|_bytes|_dir)?!|#\s*\[\s*path\b")
+_INCLUDE_CALL = re.compile(r"\b(?:include_str|include_bytes|include|include_dir)!\s*\(")
+_INCLUDE_ARG = re.compile(
+ r"\s*(concat!\s*\([^()]*(?:\([^()]*\)[^()]*)*\)|\"[^\"]*\")\s*\)",
+ re.S,
+)
+_PATH_ATTR = re.compile(r"#\s*\[\s*path\s*=\s*(?=\")")
+
+
+def _string_literals(src: str, masked: str) -> list[tuple[int, str]]:
+ """(offset of the opening quote, text) of every string literal. The mask
+ keeps the quote characters, so they pair up in order."""
+ quotes = [i for i, ch in enumerate(masked) if ch == '"']
+ return [(a, src[a + 1 : b]) for a, b in zip(quotes[::2], quotes[1::2])]
+
+
+def _in_spans(pos: int, spans: list[tuple[int, int]]) -> bool:
+ return any(a <= pos < b for a, b in spans)
+
+
+def _resolve_concat(arg: str, crate_dir: Path) -> str | None:
+ parts = []
+ for literal, env in re.findall(r'"([^"]*)"|(env!\s*\(\s*"[A-Z_]+"\s*\))', arg):
+ if env:
+ if "CARGO_MANIFEST_DIR" not in env:
+ return None # OUT_DIR and the like: not a source file
+ parts.append(str(crate_dir))
+ else:
+ parts.append(literal)
+ return "".join(parts)
+
+
+def source_reads(crate_dir: Path) -> set[Path]:
+ """Every file or directory the crate's non-test code reads at build time."""
+ reads: set[Path] = set()
+ files = []
+ for dirpath, dirnames, filenames in os.walk(crate_dir / "src"):
+ dirnames[:] = [d for d in dirnames if d != "target"]
+ files += [Path(dirpath) / f for f in filenames if f.endswith(".rs")]
+ build_rs = crate_dir / "build.rs"
+ if build_rs.exists():
+ files.append(build_rs)
+ for path in files:
+ src = path.read_text(errors="replace")
+ # The lexer is slow in Python: only files that can read something.
+ if path != build_rs and not _MAY_READ.search(src):
+ continue
+ masked = _mask(src)
+ spans = test_spans(src, masked)
+ for call in _INCLUDE_CALL.finditer(masked):
+ if _in_spans(call.start(), spans):
+ continue
+ arg = _INCLUDE_ARG.match(src, call.end())
+ if not arg:
+ continue
+ text = arg.group(1)
+ if text.startswith("concat"):
+ resolved = _resolve_concat(text, crate_dir)
+ if resolved is None:
+ continue
+ reads.add(Path(os.path.normpath(resolved)))
+ else:
+ reads.add(Path(os.path.normpath(path.parent / text.strip('"'))))
+ for attr in _PATH_ATTR.finditer(masked):
+ if _in_spans(attr.start(), spans):
+ continue
+ literal = next((t for a, t in _string_literals(src, masked) if a == attr.end()), None)
+ if literal:
+ reads.add(Path(os.path.normpath(path.parent / literal)))
+ if path == build_rs:
+ # A build script runs with the crate directory as its working
+ # directory and joins "../..." literals onto CARGO_MANIFEST_DIR.
+ for offset, literal in _string_literals(src, masked):
+ if literal.startswith("../") and not _in_spans(offset, spans):
+ reads.add(Path(os.path.normpath(crate_dir / literal)))
+ return reads
+
+
+# ----------------------------------------------------------------------------
+# The key
+
+
+def _git_ls_files(root: Path, specs: list[str], stage: bool = True) -> list[str]:
+ out = subprocess.run(
+ ["git", "-c", "core.quotepath=off", "ls-files", *(["-s"] if stage else []), "-z", "--", *specs],
+ cwd=root,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ return [e for e in out.stdout.split("\0") if e]
+
+
+def _is_git_internal(rel: str) -> bool:
+ return rel == ".git" or rel.startswith(".git/") or "/.git/" in rel
+
+
+def _covered_by_base(rel: str) -> bool:
+ """Is a tracked path inside the hashed part of libs/cua?
+
+ Decided here, not with `:(exclude)` pathspecs: git's `:(exclude,glob)**/*.md`
+ did not exclude libs/cua's Markdown, so the earlier key rebuilt on every
+ README edit."""
+ if not rel.startswith(WORKSPACE + "/"):
+ return False
+ for excluded in HASHED_OUT:
+ if rel == f"{WORKSPACE}/{excluded}" or rel.startswith(f"{WORKSPACE}/{excluded}/"):
+ return False
+ return not rel.endswith(".md")
+
+
+def extra_inputs(root: Path) -> list[str]:
+ """Repository paths the build reads that the hashed part of libs/cua lacks.
+
+ Files and directories, sorted, relative to the repository root. A directory
+ is listed once; `git ls-files` expands it."""
+ extra: set[str] = set()
+ for crate_dir in closure(root).values():
+ for read in source_reads(crate_dir):
+ try:
+ rel = _rel(root, read)
+ except ValueError:
+ continue
+ if rel.startswith("../") or _is_git_internal(rel):
+ continue
+ if (root / rel).is_dir():
+ files = _git_ls_files(root, [f":(literal){rel}"], stage=False)
+ if files and all(_covered_by_base(f) for f in files):
+ continue
+ elif _covered_by_base(rel):
+ continue
+ extra.add(rel)
+ for config_dir in CARGO_CONFIG_DIRS:
+ if (root / config_dir).is_dir():
+ extra.add(config_dir)
+ return sorted(extra)
+
+
+def listing(root: Path) -> list[str]:
+ """The sorted, de-duplicated `git ls-files -s` lines the key hashes."""
+ lines = {
+ line
+ for line in _git_ls_files(root, [WORKSPACE])
+ if _covered_by_base(line.split("\t", 1)[1])
+ }
+ extra = extra_inputs(root)
+ if extra:
+ lines |= set(_git_ls_files(root, [f":(literal){p}" for p in extra]))
+ return sorted(lines, key=lambda line: line.split("\t", 1)[1])
+
+
+def hash_listing(lines: list[str]) -> str:
+ payload = "\n".join(sorted(set(lines), key=lambda line: line.split("\t", 1)[1])) + "\n"
+ return hashlib.sha256((KEY_VERSION + "\n" + payload).encode()).hexdigest()
+
+
+def key(root: Path) -> str:
+ return hash_listing(listing(root))
+
+
+# ----------------------------------------------------------------------------
+# Ground truth from a finished build
+
+
+def _depinfo_paths(text: str) -> set[str]:
+ paths: set[str] = set()
+ for line in text.splitlines():
+ line = line.rstrip("\\ ").strip()
+ if not line:
+ continue
+ # `target/.../name.d` lists `output: input input ...`; later lines are
+ # `input:` entries or empty rules.
+ if ": " in line and not line.startswith(" "):
+ line = line.split(": ", 1)[1]
+ line = line.rstrip(":")
+ token = ""
+ escaped = False
+ for ch in line:
+ if escaped:
+ token += ch
+ escaped = False
+ elif ch == "\\":
+ escaped = True
+ elif ch == " ":
+ if token:
+ paths.add(token)
+ token = ""
+ else:
+ token += ch
+ if token:
+ paths.add(token)
+ return {p.rstrip(":") for p in paths}
+
+
+def build_reads(root: Path, target_dir: Path) -> set[str]:
+ """Repository-relative files a finished build read (rustc dep-info and build
+ script `rerun-if-changed` lines), outside target/ and the registry."""
+ found: set[str] = set()
+ for d_file in (target_dir / "deps").glob("*.d"):
+ for p in _depinfo_paths(d_file.read_text(errors="replace")):
+ found.add(p)
+ members = _workspace_members(root)
+ for output in (target_dir / "build").glob("*/output"):
+ crate_name = output.parent.name.rsplit("-", 1)[0]
+ crate_dir = members.get(crate_name)
+ for line in output.read_text(errors="replace").splitlines():
+ m = re.match(r"cargo:rerun-if-changed=(.*)", line)
+ if not m:
+ continue
+ p = m.group(1)
+ if not os.path.isabs(p) and crate_dir is not None:
+ p = str(crate_dir / p)
+ found.add(p)
+ repo: set[str] = set()
+ for p in found:
+ path = Path(p)
+ if not path.is_absolute():
+ path = root / WORKSPACE / path # rustc runs from the workspace root
+ try:
+ rel = _rel(root, Path(os.path.normpath(path)))
+ except ValueError:
+ continue
+ if rel.startswith("../") or "/target/" in f"/{rel}" or _is_git_internal(rel):
+ continue
+ repo.add(rel)
+ return repo
+
+
+def verify(root: Path, target_dir: Path) -> list[str]:
+ """Tracked files the build read that the key does not hash."""
+ hashed = {line.split("\t", 1)[1] for line in listing(root)}
+ tracked = set(
+ subprocess.run(
+ ["git", "-c", "core.quotepath=off", "ls-files", "-z"],
+ cwd=root,
+ capture_output=True,
+ text=True,
+ check=True,
+ ).stdout.split("\0")
+ )
+ missing: set[str] = set()
+ for rel in sorted(build_reads(root, target_dir)):
+ if (root / rel).is_dir():
+ # `rerun-if-changed` on a directory: every file in it is read.
+ candidates = [t for t in tracked if t.startswith(rel + "/")]
+ else:
+ candidates = [rel] if rel in tracked else []
+ missing.update(c for c in candidates if c not in hashed)
+ return sorted(missing)
+
+
+def main(argv: list[str]) -> int:
+ if len(argv) < 2 or argv[1] not in {"key", "inputs", "closure", "verify"}:
+ print(__doc__)
+ return 2
+ root = repo_root()
+ command = argv[1]
+ if command == "key":
+ print(key(root))
+ elif command == "inputs":
+ print("\n".join(extra_inputs(root)))
+ elif command == "closure":
+ print("\n".join(sorted(closure(root))))
+ else:
+ if len(argv) != 3:
+ print("usage: verify <target dir>", file=sys.stderr)
+ return 2
+ missing = verify(root, Path(argv[2]).resolve())
+ if missing:
+ for rel in missing:
+ print(f"::error::the build read {rel}, which the slice cache key does not cover")
+ print(
+ "Add it to extra_inputs() in .github/scripts/spaces_rust_slice_inputs.py "
+ "(or fix the scanner) and to the paths of ci-cua-spaces-rust-slice.yml.",
+ file=sys.stderr,
+ )
+ return 1
+ print("The slice cache key covers every tracked file the build read.")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv))
diff --git a/.github/scripts/tests/test_ci_cua_spaces_macos_workflow.py b/.github/scripts/tests/test_ci_cua_spaces_macos_workflow.py
new file mode 100644
index 0000000000..bd9fb53992
--- /dev/null
+++ b/.github/scripts/tests/test_ci_cua_spaces_macos_workflow.py
@@ -0,0 +1,133 @@
+"""The SwiftUI app's CI builds the Rust library once and checks it in parallel.
+
+`ci-cua-spaces-macos.yml` used to be one 60 minute job: compile
+libcua_spaces_ffi, then run every Swift check one after the other, behind a
+`core` job it waited for. It is now `ffi` (build once, or take main's compiled
+library) plus three jobs that download the library and run side by side. These
+tests keep the split honest: every check still runs, none compiles the library
+again, and none waits for `core`.
+"""
+
+from pathlib import Path
+
+import yaml
+
+
+ROOT = Path(__file__).resolve().parents[3]
+WORKFLOW = yaml.safe_load((ROOT / ".github/workflows/ci-cua-spaces-macos.yml").read_text())
+JOBS = WORKFLOW["jobs"]
+SWIFT_JOBS = ("swift-tests", "bindings", "app-bundle")
+ARTIFACT = "cua-spaces-ffi-dylib"
+
+# Every check the single SwiftUI job ran, and the job that now runs it.
+CHECKS = {
+ "libs/spaces-app-swift/scripts/test.sh": "swift-tests",
+ "libs/spaces-notch-swift/scripts/test.sh": "swift-tests",
+ "scripts/test.sh\n": "swift-tests",
+ "--filter BridgeContractTests": "swift-tests",
+ "npx vitest run src/bridge/__tests__/contract.test.ts": "swift-tests",
+ "generate-bindings.mjs --check --skip-build": "bindings",
+ "pnpm native -- --skip-build --check-bindings": "bindings",
+ "scripts/build-app.sh release": "app-bundle",
+ "scripts/appcast-dry-run.sh": "app-bundle",
+ "xcodebuild test": "app-bundle",
+}
+
+
+def run_text(job: str) -> str:
+ return "\n".join(step.get("run", "") + "\n" for step in JOBS[job]["steps"])
+
+
+def uses(job: str) -> list[str]:
+ return [step["uses"] for step in JOBS[job]["steps"] if "uses" in step]
+
+
+def test_every_swift_check_still_runs_in_exactly_one_job():
+ for needle, owner in CHECKS.items():
+ holders = [name for name in JOBS if needle in run_text(name)]
+ assert holders == [owner], f"{needle!r} runs in {holders}, expected [{owner!r}]"
+
+
+def test_the_library_is_compiled_once_and_downloaded_by_the_rest():
+ builders = [n for n in JOBS if "cargo build" in run_text(n) and "cua-spaces-ffi" in run_text(n)]
+ assert builders == ["ffi"]
+ for name in SWIFT_JOBS:
+ job = JOBS[name]
+ assert job["needs"] == "ffi", name
+ download = [s for s in job["steps"] if str(s.get("uses", "")).startswith("actions/download-artifact")]
+ assert len(download) == 1 and download[0]["with"]["name"] == ARTIFACT, name
+ assert download[0]["with"]["path"] == "libs/cua/target/release", name
+ upload = [s for s in JOBS["ffi"]["steps"] if str(s.get("uses", "")).startswith("actions/upload-artifact")]
+ assert len(upload) == 1 and upload[0]["with"]["name"] == ARTIFACT
+ assert upload[0]["with"]["if-no-files-found"] == "error"
+
+
+def test_the_jobs_that_load_the_library_stage_it_after_downloading_it():
+ for name in ("swift-tests", "app-bundle"):
+ names = [s.get("name", "") for s in JOBS[name]["steps"]]
+ assert names.index("Stage the Cua Spaces app library") > names.index("Download the Cua Spaces app library")
+ assert "stage-library.sh" in run_text(name)
+
+
+def test_the_swift_checks_do_not_wait_for_core():
+ # `core` is a 4 minute Linux job; waiting for it put 4 minutes in front of
+ # the 60 minute macOS path. It still runs, and ci-gate still waits for it.
+ assert "core" in JOBS
+ for name in ("ffi", *SWIFT_JOBS):
+ assert "core" not in str(JOBS[name].get("needs", "")), name
+
+
+def test_release_please_pull_requests_still_skip_the_macos_jobs():
+ # `swift` used to inherit core's skip through `needs`; `ffi` carries it now
+ # and the rest are skipped with it.
+ assert "release-please--branches--" in JOBS["ffi"]["if"]
+ assert "release-please--branches--" in JOBS["core"]["if"]
+
+
+def test_ffi_takes_main_slice_only_by_restoring_it():
+ steps = {s.get("id"): s for s in JOBS["ffi"]["steps"] if s.get("id")}
+ slice_step = steps["slice"]
+ assert slice_step["uses"] == "./.github/actions/spaces-rust-slice"
+ assert slice_step["with"] == {"triple": "aarch64-apple-darwin", "build": "false"}
+ assert JOBS["ffi"]["env"]["CUA_RELEASE_REPOSITORY"] == "${{ github.repository }}"
+ # Every compile step is for the miss path only.
+ for step in JOBS["ffi"]["steps"]:
+ if step.get("name") in ("Install Rust (toolchain from rust-toolchain.toml)", "Install protoc",
+ "Build the Cua Spaces app library") or step.get("uses") == "./.github/actions/rust-cache":
+ assert step["if"] == "steps.slice.outputs.cache-hit != 'true'", step
+ copy = next(s for s in JOBS["ffi"]["steps"] if s.get("name") == "Use the restored library")
+ assert copy["if"] == "steps.slice.outputs.cache-hit == 'true'"
+ assert "target/aarch64-apple-darwin/release/libcua_spaces_ffi.dylib" in copy["run"]
+
+
+def test_only_ffi_saves_the_sccache_family_on_main():
+ rust_cache = [s for s in JOBS["bindings"]["steps"] if s.get("uses") == "./.github/actions/rust-cache"]
+ assert len(rust_cache) == 1 and rust_cache[0]["with"]["save-if"] == "false"
+ for name in ("swift-tests", "app-bundle"):
+ assert "./.github/actions/rust-cache" not in uses(name)
+
+
+def test_snapshot_differences_are_still_uploaded_on_failure():
+ step = next(s for s in JOBS["swift-tests"]["steps"] if s.get("name") == "Upload snapshot differences")
+ assert step["if"] == "failure()"
+
+
+def test_the_slice_action_builds_by_default_and_restores_only_on_request():
+ action = yaml.safe_load((ROOT / ".github/actions/spaces-rust-slice/action.yml").read_text())
+ assert action["inputs"]["build"]["default"] == "true"
+ steps = action["runs"]["steps"]
+ by_id = {s.get("id"): s for s in steps if s.get("id")}
+ assert by_id["cache"]["uses"].startswith("actions/cache@") and by_id["cache"]["if"] == "inputs.build == 'true'"
+ assert by_id["restore"]["uses"].startswith("actions/cache/restore@")
+ assert by_id["restore"]["if"] == "inputs.build != 'true'"
+ # Nothing compiles unless build is on.
+ for step in steps:
+ if step.get("name") in ("Install protoc", "Build the slice") or str(step.get("uses", "")).startswith(
+ ("dtolnay/rust-toolchain", "Swatinem/rust-cache")
+ ):
+ assert step["if"].startswith("inputs.build == 'true' &&"), step
+ # The producer and the release keep their behaviour: they pass no `build`.
+ for workflow in ("ci-cua-spaces-rust-slice.yml", "cd-cua-spaces.yml", "cd-cua-spaces-build.yml"):
+ text = (ROOT / ".github/workflows" / workflow).read_text()
+ assert "spaces-rust-slice" in text
+ assert 'build: "false"' not in text
diff --git a/.github/scripts/tests/test_spaces_rust_slice_inputs.py b/.github/scripts/tests/test_spaces_rust_slice_inputs.py
new file mode 100644
index 0000000000..1eb77e8043
--- /dev/null
+++ b/.github/scripts/tests/test_spaces_rust_slice_inputs.py
@@ -0,0 +1,353 @@
+"""The compiled SwiftUI Rust slice is keyed by everything its build reads.
+
+`ci-cua-spaces-macos.yml` restores main's compiled libcua_spaces_ffi when a pull
+request's cache key equals the one main built. That is only safe if the key
+changes whenever any file the compiler or a build script reads changes. These
+tests pin that: the inputs are derived from the code (not listed by hand), any
+change to one of them changes the key, the producer workflow triggers on each
+of them, and the producer checks the claim against a real build.
+"""
+
+import hashlib
+import importlib.util
+import re
+import subprocess
+from pathlib import Path
+
+import pytest
+import yaml
+
+
+ROOT = Path(__file__).resolve().parents[3]
+SCRIPT = ROOT / ".github/scripts/spaces_rust_slice_inputs.py"
+spec = importlib.util.spec_from_file_location("spaces_rust_slice_inputs", SCRIPT)
+inputs = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(inputs)
+
+ACTION = yaml.safe_load((ROOT / ".github/actions/spaces-rust-slice/action.yml").read_text())
+PRODUCER = yaml.safe_load((ROOT / ".github/workflows/ci-cua-spaces-rust-slice.yml").read_text())
+MACOS = yaml.safe_load((ROOT / ".github/workflows/ci-cua-spaces-macos.yml").read_text())
+
+needs_toml = pytest.mark.skipif(inputs._load_toml is None, reason="no TOML parser for the dependency closure")
+
+
+def tracked(*specs):
+ out = subprocess.run(
+ ["git", "ls-files", "-z", "--", *specs], cwd=ROOT, capture_output=True, text=True, check=True
+ )
+ return [p for p in out.stdout.split("\0") if p]
+
+
+def glob_matches(pattern, path):
+ """GitHub's `paths:` globs: `**` crosses directories, `*` does not."""
+ regex = ""
+ i = 0
+ while i < len(pattern):
+ if pattern.startswith("**/", i):
+ regex += "(?:.*/)?"
+ i += 3
+ elif pattern.startswith("**", i):
+ regex += ".*"
+ i += 2
+ elif pattern[i] == "*":
+ regex += "[^/]*"
+ i += 1
+ else:
+ regex += re.escape(pattern[i])
+ i += 1
+ return re.fullmatch(regex, path) is not None
+
+
[email protected](scope="module")
+def extra():
+ return inputs.extra_inputs(ROOT)
+
+
[email protected](scope="module")
+def lines():
+ return inputs.listing(ROOT)
+
+
+def key_of(lines):
+ return inputs.hash_listing(lines)
+
+
+def with_changed_blob(lines, path):
+ out = []
+ hit = False
+ for line in lines:
+ meta, name = line.split("\t", 1)
+ if name == path:
+ mode, oid, stage = meta.split()
+ oid = hashlib.sha1(oid.encode()).hexdigest() # another blob
+ line = f"{mode} {oid} {stage}\t{name}"
+ hit = True
+ out.append(line)
+ assert hit, f"{path} is not in the key listing"
+ return out
+
+
+# ---------------------------------------------------------------- the inputs
+
+
+@needs_toml
+def test_the_closure_is_what_the_two_packages_build():
+ crates = inputs.closure(ROOT)
+ for name in (
+ "cua-spaces-ffi",
+ "cua-spaces-cli",
+ "cua-spaces-app-core",
+ "cua-sdk",
+ "cua-image",
+ "cua-host",
+ "cua-agent-setup",
+ "cua-telemetry",
+ ):
+ assert name in crates, name
+ # A workspace crate nothing in the slice depends on is not scanned.
+ assert "cua-bindgen" in inputs._workspace_members(ROOT)
+ assert "cua-bindgen" not in crates
+
+
+@needs_toml
+def test_the_files_outside_the_hashed_tree_are_derived_from_the_code(extra):
+ # palette.rs embeds it with include_str!; the reviewer's finding.
+ assert "apps/cua-spaces-web/src/lib/keybindings.json" in extra
+ # wizard.rs and cua-cli include it; cua-image and cua-telemetry's build scripts copy it.
+ assert "libs/images/sandbox-images.json" in extra
+ # cua-host's build script.
+ assert "libs/cua-spacesd/VERSION" in extra
+ # cua-agent-setup's build script embeds the skills; the hash drops libs/cua Markdown.
+ assert "libs/cua/skills" in extra
+ # Cargo merges the repository's .cargo/config.toml into every build below it.
+ assert ".cargo" in extra
+ # Read only by unit tests (`#[cfg(test)]`, `#[test]`) or tests/: not in the library.
+ for test_only in (
+ "docs/content/docs/cua-sdk/guides/your-cloud-safety.mdx",
+ "scripts/images/check-tag-safety.sh",
+ "scripts/images/manifests/macos-26.json",
+ "libs/images/runtime-gvisor/versions.env",
+ "tests/compat/e2b/fixtures/signature-vectors.json",
+ ):
+ assert test_only not in extra
+ for path in extra:
+ assert tracked(path), f"{path} is not tracked"
+
+
+@needs_toml
+def test_nothing_inside_libs_cua_is_left_out_unless_the_build_reads_it(extra):
+ # The skills (embedded by cua-agent-setup) and the proto directory (the
+ # contract the build compiles) hold Markdown the hash would otherwise drop.
+ inside = [p for p in extra if p.startswith("libs/cua/")]
+ assert inside == ["libs/cua/proto", "libs/cua/skills"]
+
+
+# ------------------------------------------------------------------ the key
+
+
+@needs_toml
+def test_changing_any_input_changes_the_key(lines, extra):
+ base = key_of(lines)
+ paths = {line.split("\t", 1)[1] for line in lines}
+ checked = 0
+ for entry in extra:
+ for path in tracked(entry):
+ assert path in paths, f"{path} (an input) is missing from the key's listing"
+ assert key_of(with_changed_blob(lines, path)) != base, path
+ checked += 1
+ assert checked >= 10
+ # And a file that is in the hashed tree.
+ first = next(p for p in sorted(paths) if p.startswith("libs/cua/crates/cua-sdk/src/"))
+ assert key_of(with_changed_blob(lines, first)) != base
+
+
+@needs_toml
+def test_the_pull_request_that_only_edits_one_of_them_misses(lines):
+ """The reviewer's case: keybindings.json or sandbox-images.json alone."""
+ for path in ("apps/cua-spaces-web/src/lib/keybindings.json", "libs/images/sandbox-images.json"):
+ assert key_of(with_changed_blob(lines, path)) != key_of(lines)
+
+
+@needs_toml
+def test_what_the_build_does_not_read_does_not_change_the_key(lines):
+ paths = {line.split("\t", 1)[1] for line in lines}
+ # A README or changelog edit must not cost a rebuild (`:(exclude,glob)**/*.md`
+ # in the old key did not exclude them).
+ assert "libs/cua/README.md" not in paths and "libs/cua/CHANGELOG.md" not in paths
+ assert not any(p.startswith("libs/cua/crates/") and p.endswith(".md") for p in paths)
+ assert not any(p.startswith(("libs/cua/python/", "libs/cua/typescript/", "libs/cua/kotlin/")) for p in paths)
+ # The skills' Markdown is read (embedded), so it is hashed.
+ assert any(p.startswith("libs/cua/skills/") and p.endswith(".md") for p in paths)
+ # Order and duplicates do not matter.
+ assert key_of(list(reversed(lines)) + lines[:3]) == key_of(lines)
+
+
+def test_the_action_keys_the_cache_on_the_script():
+ steps = {s.get("id"): s for s in ACTION["runs"]["steps"] if s.get("id")}
+ run = steps["key"]["run"]
+ assert "python3 .github/scripts/spaces_rust_slice_inputs.py key" in run
+ assert "git ls-files" not in run # no second, hand-kept copy of the input list
+ assert re.search(r"swiftui-rust-v3-.*\$\{inputs_hash\}", run)
+
+
+# --------------------------------------------------------------- the triggers
+
+
+@needs_toml
+def test_the_producer_runs_on_main_when_any_input_changes(extra):
+ patterns = PRODUCER[True]["push"]["paths"]
+ for entry in extra:
+ for path in tracked(entry):
+ assert any(glob_matches(p, path) for p in patterns), f"{path} does not trigger the producer"
+ for path in ("libs/cua/crates/cua-sdk/src/lib.rs", ".github/scripts/spaces_rust_slice_inputs.py"):
+ assert any(glob_matches(p, path) for p in patterns), path
+
+
+def test_the_producer_and_the_consumer_run_when_the_key_logic_changes():
+ script = ".github/scripts/spaces_rust_slice_inputs.py"
+ assert script in PRODUCER[True]["pull_request"]["paths"]
+ assert script in MACOS[True]["pull_request"]["paths"]
+ assert script in MACOS[True]["push"]["paths"]
+
+
+def test_the_macos_checks_still_run_for_the_two_files_that_started_this():
+ for event in ("pull_request", "push"):
+ paths = MACOS[True][event]["paths"]
+ assert "apps/cua-spaces-web/src/lib/keybindings.json" in paths
+ assert "libs/images/sandbox-images.json" in paths
+
+
+# ---------------------------------------------------------- the ground truth
+
+
+def test_the_producer_verifies_the_key_against_its_build_and_the_release_does_not():
+ producer_step = next(
+ s for s in PRODUCER["jobs"]["slice"]["steps"] if s.get("uses") == "./.github/actions/spaces-rust-slice"
+ )
+ assert producer_step["with"]["verify-key"] == "true"
+ assert ACTION["inputs"]["verify-key"]["default"] == "false"
+ for workflow in ("cd-cua-spaces.yml", "cd-cua-spaces-build.yml", "ci-cua-spaces-macos.yml"):
+ assert "verify-key" not in (ROOT / ".github/workflows" / workflow).read_text()
+ verify = next(s for s in ACTION["runs"]["steps"] if s.get("name", "").startswith("The cache key covers"))
+ assert "inputs.verify-key == 'true'" in verify["if"] and "inputs.build == 'true'" in verify["if"]
+ assert "spaces_rust_slice_inputs.py verify" in verify["run"]
+
+
+@needs_toml
+def test_verify_reports_a_file_the_build_read_and_the_key_misses(tmp_path):
+ deps = tmp_path / "deps"
+ build = tmp_path / "build" / "cua-image-0123456789abcdef"
+ deps.mkdir()
+ build.mkdir(parents=True)
+ hashed = "crates/cua-sdk/src/lib.rs"
+ missed = "../../docs/content/docs/cua-sdk/guides/your-cloud-safety.mdx"
+ (deps / "cua_sdk-1.d").write_text(
+ f"{tmp_path}/deps/cua_sdk-1.rmeta: {hashed} {missed}\n\n{hashed}:\n{missed}:\n"
+ )
+ (build / "output").write_text(
+ f"cargo:rerun-if-changed={ROOT}/libs/cua/crates/cua-image/../../../images/sandbox-images.json\n"
+ "cargo:rerun-if-changed=../../.git/HEAD\n"
+ )
+ assert inputs.verify(ROOT, tmp_path) == ["docs/content/docs/cua-sdk/guides/your-cloud-safety.mdx"]
+
+
+@needs_toml
+def test_verify_is_quiet_when_the_key_covers_everything(tmp_path):
+ (tmp_path / "deps").mkdir()
+ (tmp_path / "deps" / "app_core-1.d").write_text(
+ "out.rmeta: crates/cua-spaces-app-core/src/palette.rs "
+ "../../apps/cua-spaces-web/src/lib/keybindings.json "
+ "../../libs/images/sandbox-images.json /Users/runner/.cargo/registry/src/x/serde.rs\n"
+ )
+ assert inputs.verify(ROOT, tmp_path) == []
+
+
+@needs_toml
+def test_verify_expands_a_directory_the_build_script_watches(tmp_path):
+ build = tmp_path / "build" / "cua-agent-setup-0123456789abcdef"
+ build.mkdir(parents=True)
+ (tmp_path / "deps").mkdir()
+ # The skills (hashed, Markdown included) and a directory the key does not cover.
+ (build / "output").write_text(
+ f"cargo:rerun-if-changed={ROOT}/libs/cua/crates/cua-agent-setup/../../skills\n"
+ f"cargo:rerun-if-changed={ROOT}/scripts/images/manifests\n"
+ )
+ missing = inputs.verify(ROOT, tmp_path)
+ assert missing and all(m.startswith("scripts/images/manifests/") for m in missing)
+
+
+def test_dep_info_with_escaped_spaces_and_empty_rules():
+ text = "target/x.rmeta: a/b.rs c\\ d.rs \\\n e.rs\n\na/b.rs:\nc\\ d.rs:\n"
+ assert inputs._depinfo_paths(text) == {"a/b.rs", "c d.rs", "e.rs"}
+
+
+# -------------------------------------------------------------- the scanner
+
+
+def reads_of(tmp_path, files, name="crate"):
+ crate = tmp_path / name
+ (crate / "src").mkdir(parents=True, exist_ok=True)
+ for name, text in files.items():
+ (crate / name).write_text(text)
+ return {str(p.relative_to(tmp_path)) for p in inputs.source_reads(crate)}
+
+
+def test_a_read_in_ordinary_code_counts_and_one_in_a_test_module_does_not(tmp_path):
+ src = (
+ 'const A: &str = include_str!("../../a.json");\n'
+ "#[cfg(test)]\nmod tests {\n"
+ ' const B: &str = include_str!("../../b.json");\n'
+ ' fn f() { let s = "}"; let c = \'}\'; let _ = include_str!("../../c.json"); }\n'
+ "}\n"
+ 'const D: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/d.bin"));\n'
+ '#[test]\nfn t() { include_str!("../../e.json"); }\n'
+ 'const OUT: &str = include_str!(concat!(env!("OUT_DIR"), "/generated.rs"));\n'
+ )
+ assert reads_of(tmp_path, {"src/lib.rs": src}) == {"a.json", "crate/d.bin"}
+
+
+def test_text_that_only_looks_like_code_is_not_a_read_or_a_test_boundary(tmp_path):
+ src = (
+ '// include_str!("../../comment.json")\n'
+ '/* #[cfg(test)] */\n'
+ 'const R: &str = r#"include_str!("../../raw.json") #[cfg(test)] {"#;\n'
+ 'fn after() { let _ = include_str!("../../after.json"); }\n'
+ )
+ assert reads_of(tmp_path, {"src/lib.rs": src}) == {"after.json"}
+
+
+def test_a_cfg_test_item_without_a_body_and_an_inner_attribute(tmp_path):
+ assert reads_of(
+ tmp_path,
+ {"src/lib.rs": '#[cfg(test)]\nmod tests;\nconst A: &str = include_str!("../../a.json");\n'},
+ ) == {"a.json"}
+ assert (
+ reads_of(
+ tmp_path,
+ {"src/lib.rs": '#![cfg(test)]\nconst A: &str = include_str!("../../a.json");\n'},
+ name="inner",
+ )
+ == set()
+ )
+
+
+def test_cfg_not_test_and_cfg_any_test_are_code_that_ships(tmp_path):
+ src = (
+ '#[cfg(not(test))]\nconst A: &str = include_str!("../../a.json");\n'
+ '#[cfg(any(test, feature = "x"))]\nconst B: &str = include_str!("../../b.json");\n'
+ )
+ assert reads_of(tmp_path, {"src/lib.rs": src}) == {"a.json", "b.json"}
+
+
+def test_a_build_script_reads_every_dot_dot_literal_and_path_attributes(tmp_path):
+ build = (
+ 'const FILES: &[(&str, &str)] = &[("../x.json", "x.json"), ("../skills", "skills")];\n'
+ 'fn main() { let _ = "not/a/parent"; let _ = "../.git/HEAD"; }\n'
+ )
+ lib = '#[path = "../shared/mod.rs"]\nmod shared;\n'
+ assert reads_of(tmp_path, {"build.rs": build, "src/lib.rs": lib}) == {
+ "x.json",
+ "skills",
+ ".git/HEAD",
+ "crate/shared/mod.rs",
+ }
diff --git a/.github/workflows/ci-cua-spaces-macos.yml b/.github/workflows/ci-cua-spaces-macos.yml
index 17195a64c9..dbe408e644 100644
--- a/.github/workflows/ci-cua-spaces-macos.yml
+++ b/.github/workflows/ci-cua-spaces-macos.yml
@@ -10,6 +10,15 @@ name: "CI: Cua Spaces for macOS (SwiftUI)"
# check), and the XCUITests. Unsigned; the only secret is the optional
# Sparkle key, for the appcast dry run. The Keyvault's live paths need a daemon and are not
# exercised here (the view models run against a fake broker).
+#
+# Shape: the Rust library is built once (`ffi`; 20 to 60 minutes cold) and the
+# Swift checks download it and run side by side (`swift-tests`, `bindings`,
+# `app-bundle`), so the wall clock is the build plus the slowest of them, not
+# the sum of every step. `ffi` takes main's compiled library from the Actions
+# cache (the one ci-cua-spaces-rust-slice.yml produces, see
+# .github/actions/spaces-rust-slice) when the pull request leaves the Rust
+# inputs as they are on main, and compiles otherwise. `core` runs beside them
+# (the macOS jobs do not wait for it).
on:
pull_request:
@@ -30,6 +39,10 @@ on:
- "libs/spaces-notch-swift/**"
- "libs/images/sandbox-images.json"
- "libs/cua/crates/cua-icon-cache/**"
+ # `ffi` restores main's compiled library through this action, keyed by
+ # what that script says the build reads.
+ - ".github/actions/spaces-rust-slice/**"
+ - ".github/scripts/spaces_rust_slice_inputs.py"
- ".github/workflows/ci-cua-spaces-macos.yml"
push:
branches: [main]
@@ -50,6 +63,10 @@ on:
- "libs/spaces-notch-swift/**"
- "libs/images/sandbox-images.json"
- "libs/cua/crates/cua-icon-cache/**"
+ # `ffi` restores main's compiled library through this action, keyed by
+ # what that script says the build reads.
+ - ".github/actions/spaces-rust-slice/**"
+ - ".github/scripts/spaces_rust_slice_inputs.py"
- ".github/workflows/ci-cua-spaces-macos.yml"
workflow_dispatch:
@@ -95,31 +112,71 @@ jobs:
cargo fmt -p cua-spaces-app-core -- --check
cargo clippy --locked -p cua-spaces-app-core --all-targets --features keyvault-client -- -D warnings
- swift:
- name: SwiftUI app (swift test, bundle, XCUITest)
- needs: core
+ # Builds libcua_spaces_ffi once. The Swift jobs below download it instead of
+ # each compiling it (the checks that need the Rust toolchain compile only
+ # the small bindgen tools).
+ ffi:
+ if: ${{ !startsWith(github.head_ref, 'release-please--branches--') }}
+ name: Cua Spaces app library (built once)
runs-on: macos-26
env:
CARGO_BUILD_JOBS: 4
+ # The slice cache key names the repository (same value as the producer).
+ CUA_RELEASE_REPOSITORY: ${{ github.repository }}
steps:
- uses: actions/checkout@v4
+ # Restore only: main compiled this exact Rust input tree when it merged
+ # (ci-cua-spaces-rust-slice.yml) unless the pull request changes it.
+ - name: Restore main's compiled library for these Rust inputs
+ id: slice
+ uses: ./.github/actions/spaces-rust-slice
+ with:
+ triple: aarch64-apple-darwin
+ build: "false"
+ - name: Use the restored library
+ if: steps.slice.outputs.cache-hit == 'true'
+ run: |
+ mkdir -p libs/cua/target/release
+ cp libs/cua/target/aarch64-apple-darwin/release/libcua_spaces_ffi.dylib libs/cua/target/release/
- name: Install Rust (toolchain from rust-toolchain.toml)
+ if: steps.slice.outputs.cache-hit != 'true'
working-directory: libs/cua
run: rustup show active-toolchain || rustup toolchain install
- name: Install protoc
+ if: steps.slice.outputs.cache-hit != 'true'
uses: ./.github/actions/setup-protoc
- # The app loads the Cua Spaces app export (libcua_spaces_ffi, which
- uses: ./.github/actions/rust-cache
+ if: steps.slice.outputs.cache-hit != 'true'
with:
workspaces: libs/cua
- # carries the cua SDK too), staged as libs/cua/swift/lib/libcua_sdk.dylib.
- - name: Build and stage the Cua Spaces app library
+ # The Cua Spaces app export (libcua_spaces_ffi, which carries the cua
+ # SDK too); each Swift job stages it as libs/cua/swift/lib/libcua_sdk.dylib.
+ - name: Build the Cua Spaces app library
+ if: steps.slice.outputs.cache-hit != 'true'
working-directory: libs/cua
- run: |
- cargo build --locked --release -p cua-spaces-ffi
- ../spaces-app-swift/scripts/stage-library.sh
- - name: The Cua Spaces app binding is up to date
- run: node libs/spaces-app-swift/scripts/generate-bindings.mjs --check --skip-build
+ run: cargo build --locked --release -p cua-spaces-ffi
+ - name: Upload the library for the Swift jobs
+ uses: actions/upload-artifact@v4
+ with:
+ name: cua-spaces-ffi-dylib
+ path: libs/cua/target/release/libcua_spaces_ffi.dylib
+ if-no-files-found: error
+ retention-days: 1
+
+ swift-tests:
+ name: SwiftUI app (swift tests, bridge contract)
+ needs: ffi
+ runs-on: macos-26
+ steps:
+ - uses: actions/checkout@v4
+ - name: Download the Cua Spaces app library
+ uses: actions/download-artifact@v4
+ with:
+ name: cua-spaces-ffi-dylib
+ path: libs/cua/target/release
+ - name: Stage the Cua Spaces app library
+ working-directory: libs/cua
+ run: ../spaces-app-swift/scripts/stage-library.sh
- name: spaces-app-swift tests
run: libs/spaces-app-swift/scripts/test.sh
- name: Notch package tests (the views and the Electron helper's protocol)
@@ -144,6 +201,51 @@ jobs:
answers="$RUNNER_TEMP/bridge-answers.json"
(cd apps/cua-spaces-macos && CUA_BRIDGE_ANSWERS="$answers" scripts/test.sh --filter BridgeContractTests)
(cd apps/cua-spaces-web && CUA_BRIDGE_ANSWERS="$answers" npx vitest run src/bridge/__tests__/contract.test.ts)
+ - name: Upload snapshot differences
+ if: failure()
+ uses: actions/upload-artifact@v4
+ with:
+ name: cua-spaces-macos-snapshot-diffs
+ path: apps/cua-spaces-macos/Tests/CuaSpacesMacTests/Snapshots/*.actual.png
+ if-no-files-found: ignore
+ retention-days: 7
+
+ # The generated bindings checked in next to the apps (Swift and the Electron
+ # shell's TypeScript) still match the library. These run the bindgen tools
+ # with cargo, so this job has the Rust toolchain; the other Swift jobs do not.
+ bindings:
+ name: SwiftUI and Electron bindings are up to date
+ needs: ffi
+ runs-on: macos-26
+ env:
+ CARGO_BUILD_JOBS: 4
+ steps:
+ - uses: actions/checkout@v4
+ - name: Install Rust (toolchain from rust-toolchain.toml)
+ working-directory: libs/cua
+ run: rustup show active-toolchain || rustup toolchain install
+ - name: Install protoc
+ uses: ./.github/actions/setup-protoc
+ # Restore only: `ffi` is the job that saves this family on main.
+ - uses: ./.github/actions/rust-cache
+ with:
+ workspaces: libs/cua
+ save-if: "false"
+ - name: Download the Cua Spaces app library
+ uses: actions/download-artifact@v4
+ with:
+ name: cua-spaces-ffi-dylib
+ path: libs/cua/target/release
+ - name: The Cua Spaces app binding is up to date
+ run: node libs/spaces-app-swift/scripts/generate-bindings.mjs --check --skip-build
+ # pnpm from the Electron shell's packageManager ([email protected]); the
+ # root package.json pins pnpm 9 for the rest of the repo.
+ - uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
+ with:
+ package_json_file: apps/cua-spaces-desktop/package.json
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 22
# The Electron shell's TypeScript binding of the same library
# (apps/cua-spaces-desktop/src/native/generated). A stale one used to
# surface only in the release build; this fails the PR instead.
@@ -152,14 +254,21 @@ jobs:
run: |
pnpm install --frozen-lockfile
pnpm native -- --skip-build --check-bindings
- - name: Upload snapshot differences
- if: failure()
- uses: actions/upload-artifact@v4
+
+ app-bundle:
+ name: SwiftUI app (bundle, launch check, XCUITest)
+ needs: ffi
+ runs-on: macos-26
+ steps:
+ - uses: actions/checkout@v4
+ - name: Download the Cua Spaces app library
+ uses: actions/download-artifact@v4
with:
- name: cua-spaces-macos-snapshot-diffs
- path: apps/cua-spaces-macos/Tests/CuaSpacesMacTests/Snapshots/*.actual.png
- if-no-files-found: ignore
- retention-days: 7
+ name: cua-spaces-ffi-dylib
+ path: libs/cua/target/release
+ - name: Stage the Cua Spaces app library
+ working-directory: libs/cua
+ run: ../spaces-app-swift/scripts/stage-library.sh
# Embeds Sparkle and ends with scripts/check-launch.sh: every library
# resolves inside the bundle and loads, and the app starts and exits.
- name: Build the app bundle (and its launch check)
diff --git a/.github/workflows/ci-cua-spaces-rust-slice.yml b/.github/workflows/ci-cua-spaces-rust-slice.yml
index 203b978561..3e9a8a2d57 100644
--- a/.github/workflows/ci-cua-spaces-rust-slice.yml
+++ b/.github/workflows/ci-cua-spaces-rust-slice.yml
@@ -4,24 +4,36 @@ name: "CI: Cua Spaces SwiftUI Rust slice (producer)"
# the slice on main for every change to its inputs and saves it in the Actions
# cache under the key the release build (cd-cua-spaces.yml, macos-rust) computes
# from the same action, so a release restores it (seconds) instead of
-# compiling (33 to 60 minutes). Tag runs can read the default branch's cache;
-# they cannot read another tag's or a pull request's. A manual run of this
-# workflow on a branch proves the contract: run it twice and compare the
-# cache-hit notice and the job time. The release still signs and verifies the
-# app; this only supplies the unsigned compiled slice.
+# compiling (33 to 60 minutes). The key covers every file the build reads
+# (libs/cua and the files outside it that its crates embed), and a build here
+# checks that claim against what rustc and the build scripts really opened.
+# Tag runs can read the default branch's cache; they cannot read another
+# tag's or a pull request's. A manual run of this workflow on a branch proves
+# the contract: run it twice and compare the cache-hit notice and the job
+# time. The release still signs and verifies the app; this only supplies the
+# unsigned compiled slice.
on:
push:
branches: [main]
paths:
- "libs/cua/**"
+ # Files outside libs/cua that the crates of the slice read at compile
+ # time (the action's cache key hashes them; `inputs` of
+ # .github/scripts/spaces_rust_slice_inputs.py lists them, and
+ # test_spaces_rust_slice_inputs.py fails when this list falls behind).
- "libs/cua-spacesd/VERSION"
+ - "libs/images/sandbox-images.json"
+ - "apps/cua-spaces-web/src/lib/keybindings.json"
+ - ".cargo/**"
+ - ".github/scripts/spaces_rust_slice_inputs.py"
- ".github/actions/spaces-rust-slice/**"
- ".github/workflows/ci-cua-spaces-rust-slice.yml"
# A change to the producer itself proves its contract: the first run builds,
# a re-run of the same commit restores (the pull request's own cache scope).
pull_request:
paths:
+ - ".github/scripts/spaces_rust_slice_inputs.py"
- ".github/actions/spaces-rust-slice/**"
- ".github/workflows/ci-cua-spaces-rust-slice.yml"
workflow_dispatch:
@@ -54,3 +66,6 @@ jobs:
- uses: ./.github/actions/spaces-rust-slice
with:
triple: ${{ matrix.triple }}
+ # The producer is where a key that misses an input is found: it
+ # fails here, on main, instead of serving a stale library later.
+ verify-key: "true"