diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index c1467e937..85e21d5f9 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -12,8 +12,9 @@ name: pr-ci # WHICH PORTS: `detect` maps the PR's changed paths to ports with # scripts/ci-affected-ports.sh, the same mapper local-ci.yml uses. A change under a # port's directory selects that port; a change to fixtures/, spec/, scripts/, .github/, -# agent-context/ or anything unrecognised selects ALL ports (the mapper fails open); a -# docs-only change selects none and runs only the offline `gates` lane. +# agent-context/, docs/ or anything unrecognised selects ALL ports (the mapper fails +# open); only the root prose files (README.md, CHANGELOG.md, CONTRIBUTING.md, +# AGENTS.md, CLAUDE.md) select none and run only the offline `gates` lane. # # WHAT RUNS PER PORT: unit tests plus the conformance corpora, not the docker # integration suites (integration-tests.yml owns those, on tags and on demand): diff --git a/.no-mistakes.yaml b/.no-mistakes.yaml index 465397639..8420058c3 100644 --- a/.no-mistakes.yaml +++ b/.no-mistakes.yaml @@ -7,16 +7,23 @@ # conformance.yml's lanes, integration-tests.yml's Testcontainers matrix), so pinning the # gate to it runs exactly the repository's checks, the same ones GitHub runs. # -# THE SPLIT. These two commands together are exactly `scripts/ci-local.sh --quick`, cut -# along the script's own `--only` section boundaries so neither step repeats the other. -# `scripts/ci-local.sh --help` enumerates what each section contains, and -# `MO_CI_LIST_ONLY=1 scripts/ci-local.sh --only
` prints its exact step list. -# Measured green on a warm tree 2026-09-18: test 256s, lint 23s. The test step's mutation -# gate is DIFF-SCOPED (gate_completeness_diff in the script): it mutates only the -# stryker.conf.mjs `mutate` files the branch changed against its base, and SKIPs when it -# changed none. Before 2026-11-14 it ran the full pass every round, 10-45 min under load -# and most of this step's wall time. The full pass is `--only mutation`, the flagless -# run's mutation lane, and conformance.yml (tags, nightly, dispatch). +# THE SPLIT. `lint` is `--only gates`, the one lane every change runs. `test` is +# `--affected`: it lists what the branch changed against its merge base, maps the paths to +# lanes with scripts/ci-lane-map.txt, prints which lanes it selected, skipped and why, and +# runs only those (ts-fast + ts-unit for a TypeScript or web-client change; java-slow, +# python or csharp for a change confined to that port; every selectable lane for a change +# to fixtures, spec, scripts, workflows, agent-context, docs or any path the map does not +# claim; none for a change to only the root README, CHANGELOG, CONTRIBUTING, AGENTS or CLAUDE +# files, which the gates lane covers). Selected lanes run exactly as +# `--only --no-integration` does, so a lane's contents are unchanged. Force every +# lane with MO_CI_FULL=1. Before 2026-11-15 this step was always `--only ts-fast --only +# ts-unit`, so a Java, Python or C# change was validated by the TypeScript tests and by +# nothing in its own port. Measured green on a warm tree 2026-09-18: ts-fast + ts-unit +# 256s, gates 23s. The test step's mutation gate is DIFF-SCOPED (gate_completeness_diff in +# the script) and runs inside ts-fast: it mutates only the stryker.conf.mjs `mutate` files +# the branch changed against its base, and SKIPs when it changed none. That is unchanged by +# the selection. The full pass is `--only mutation`, the flagless run's mutation lane, and +# conformance.yml (tags, nightly, dispatch). # # ORDER IS LOAD-BEARING, and it is why the work is split this way round rather than one # `--quick` in `test`. The pipeline runs test BEFORE lint. The `gates` section contains @@ -39,18 +46,19 @@ # NOT all of them, which is the part worth being exact about: five invoke `node` directly # (publish-set, script-name hooks, extract field kinds, metamodel-version, migration guides), # `check-publish-intent.sh` spawns `node` internally, and `gate_positioning_claims` invokes -# bare `bun`. What holds across both kinds is the flag's safety: it cannot misfire on a -# missing dotnet/mvn/uv, because nothing in `gates`, `ts-fast` or `ts-unit` invokes them. +# bare `bun`. `gates`, `ts-fast` and `ts-unit` invoke no dotnet/mvn/uv. `--affected` can select the +# lanes that do, and there the flag is the point: a change to that port with its toolchain +# missing FAILS, where a plain run would skip it and report green. # -# WHAT IS DELIBERATELY NOT HERE — the rest of the flagless `scripts/ci-local.sh`: the C#, -# Java, Kotlin and Python conformance lanes, the full Java reactor, and -# integration-tests.yml's five-port Testcontainers matrix plus the migrate-ts / runtime-ts -# real-Postgres suites (`--only ts-slow`). Dropped for cost — they turn a ~4.5 minute step -# into a much longer one on every round — and for dependence: they need dotnet, mvn, uv and -# a live Docker daemon, which a gate host is not guaranteed to have, and neither -# `--strict-toolchains` (a missing toolchain FAILS) nor its absence (a down Docker SKIPS -# silently) gives an honest per-change signal when they are missing. docs/RELEASING.md -# requires the flagless run before `bun publish`, which is where they are enforced. +# WHAT IS DELIBERATELY NOT HERE — the docker half. `--affected` implies `--no-integration`: +# integration-tests.yml's five-port Testcontainers matrix, the migrate-ts / runtime-ts +# real-Postgres suites (`--only ts-slow`) and the cube live check need a live Docker daemon, +# which a gate host is not guaranteed to have, and a down daemon SKIPS silently without +# `--strict-toolchains`. They turn a ~4.5 minute step into a much longer one on every round. +# A port's selected lane DOES need its toolchain (dotnet, mvn, uv), and under +# `--strict-toolchains` a missing one FAILS rather than skipping, so a change to that port +# is never reported green unchecked. docs/RELEASING.md requires the flagless run before +# `bun publish`, which is where the docker half and every lane are enforced. # # NOTE: this file must be at the repo ROOT as `.no-mistakes.yaml` (NOT under # .no-mistakes/), and repo commands are read from the DEFAULT BRANCH — so nothing here @@ -61,7 +69,7 @@ # commands above already run the same script the GitHub workflows run. commands: - test: "scripts/ci-local.sh --only ts-fast --only ts-unit --strict-toolchains" + test: "scripts/ci-local.sh --affected --strict-toolchains" lint: "scripts/ci-local.sh --only gates --strict-toolchains" # Public repository: keep PR bodies short and the run intent out of them. @@ -70,5 +78,37 @@ pr: appendix: collapsed # Mark a failing test command as pre-existing when it also fails on the base. +# +# THE EVIDENCE PHASE. After `commands.test` passes, the gate ALWAYS runs an agent that derives +# end-user scenarios and drives the product itself; no setting turns that turn off. It, not +# `commands.test`, is the long part of this step: 264 recorded Test steps for this repository +# average about 43 minutes, 130 of them over 30, against 4-5 for the command itself, and the +# agent's budget is 75 minutes on this host. Its prompt is the one thing a repository controls, +# so the runbook below scopes it the way `--affected` scopes the command: by the paths the +# change touches (`path_instructions` match the changed-file list, as scripts/ci-lane-map.txt +# does). This is guidance to a model, not a switch: it narrows what the agent is told to do +# and does not bound it. The bound is the host's `test_agent_timeout`. These fields are read +# from the default branch only, so they too take effect after merge. test: base_attribution: true + instructions: | + This repository ships libraries, a code generator and command line tools. There is no server or app to start. The test command already ran the lanes this change affects, and its output begins with the lanes it selected and skipped, so do not re-run the full suites or the lanes of ports the change does not touch. Drive the changed behavior directly instead: run the affected CLI command or generator on a small fixture from the fixtures directory, or the focused tests of the module the change touches. Keep to at most three scenarios and finish in about ten minutes. Report no-surface when the change has no runtime behavior, such as documentation, comments, CI configuration or metadata. + path_instructions: + - path: "docs/**" + instructions: | + Documentation only. There is nothing to start or drive. Return no-surface at once without running anything: the test command already ran the lanes whose tests read these pages, and the gates lane checks the prose. + - path: "server/typescript/**" + instructions: | + TypeScript port. Drive the meta CLI from the workspace on a fixture, or run bun test in only the package the change touches. Do not run the Java, Python or C# suites. + - path: "client/web/**" + instructions: | + Web client packages. Run bun test in only the package the change touches and check that its built entry still bundles. Do not run the server suites. + - path: "server/java/**" + instructions: | + Java and Kotlin ports. Run mvn test for only the Maven modules the change touches, with -pl and -am, not the whole reactor. Do not run the TypeScript, Python or C# suites. + - path: "server/python/**" + instructions: | + Python port. Run pytest on only the test modules the change touches. Do not run the TypeScript, Java or C# suites. + - path: "server/csharp/**" + instructions: | + C# port. Run dotnet test on only the test project the change touches. Do not run the TypeScript, Java or Python suites. diff --git a/AGENTS.md b/AGENTS.md index 5d9f43db6..72527bce1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -129,7 +129,8 @@ adds the C#/Java/Kotlin/Python conformance lanes, the Java reactor and `integrat Testcontainers matrix. Run `--quick` before opening a PR and the full script before a tag; `MO_CI_LIST_ONLY=1 scripts/ci-local.sh [flags]` prints what a selection would run without running it. The no-mistakes validation gate runs the script for you — see `.no-mistakes.yaml`, -whose `lint` and `test` commands partition `--quick` between the two steps. That file is read +whose `test` command runs `--affected` (only the lanes the change touches, per +`scripts/ci-lane-map.txt`) and whose `lint` command runs `--only gates`. That file is read from the **default branch**, so it does nothing until it is merged to `main`. **`leak-scan` is hosted again.** `main`'s protection requires that one status, and `hygiene.yml` diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5bfc80dcb..b2eca094c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -94,11 +94,10 @@ definition of the checks: each wrapper checks out, installs the toolchains its lane needs, and calls the script, so a local run is the same check a workflow run would be. A pull request runs the leak scan (`hygiene.yml`) and `pr-ci.yml`, which runs the unit tests and conformance corpora of only the ports the PR touches (a change -under `fixtures/`, `spec/`, `scripts/`, `.github/` or `agent-context/`, or to any file +under `fixtures/`, `spec/`, `scripts/`, `.github/`, `agent-context/` or `docs/`, or to any file outside a port's directory that the selector does not recognise, touches every port; a -change only to `docs/` or to Markdown files outside those directories runs just the -offline gates). Its `pr-ci` job is green only when every selected lane is, and a newer -push cancels the superseded run. The docker integration suites run on release tags and on +change only to the root prose files above runs just the offline gates). Its `pr-ci` job is +green only when every selected lane is, and a newer push cancels the superseded run. The docker integration suites run on release tags and on manual dispatch. Run the script locally before opening or merging a PR, so nothing red reaches `main`: @@ -110,9 +109,30 @@ scripts/ci-local.sh # full parity: hygiene.yml's leak-scan, all-port scripts/ci-local.sh --quick # hygiene.yml in full + the TypeScript half of # conformance.yml; skips the C#/Java/Kotlin/Python # ports, the java reactor and the docker suite +scripts/ci-local.sh --affected # only the lanes this change needs (see below) MO_CI_LIST_ONLY=1 scripts/ci-local.sh --quick # print the steps, run nothing ``` +**`--affected` runs only what a change can break.** It lists the paths you changed since the +merge base with the base branch (committed, uncommitted and untracked), maps each to lanes +with the table in [`scripts/ci-lane-map.txt`](scripts/ci-lane-map.txt), and prints the lanes it +selected, the lanes it skipped and the rule each path matched, so a reviewer can see what was +not run. A change confined to one port runs that port (`ts-fast` and `ts-unit` for TypeScript +and the web client, `java-slow`, `python`, `csharp`); a change to `fixtures/`, `spec/`, +`scripts/` (the map included), `.github/`, `agent-context/`, anything under `docs/` (port +tests read those pages) or any path no rule claims runs every lane, because unknown means +full; a change to only the root `README.md`, `CHANGELOG.md`, `CONTRIBUTING.md`, `AGENTS.md` +or `CLAUDE.md` runs no lane. It never runs `gates` (`--only gates` does, on every change) and +never the docker half, so a change confined to the Java port skips the Java docker +integration suites, which run on release tags and manual dispatch. When more than one port +is selected their lanes run in parallel, as the full run's do, so a change is never slower +than the full run; one port runs serially. Mutation testing is as before: diff-scoped inside +`ts-fast`. `MO_CI_FULL=1 scripts/ci-local.sh --affected` forces every lane, +`MO_CI_AFFECTED_BASE=` picks the base, and `scripts/ci-select-lanes.sh []` explains +a selection without running anything. The flagless run above is unchanged and is what +[releases](docs/RELEASING.md) use. The same map drives `pr-ci.yml`'s port detection, and +`scripts/test-ci-lane-selection.sh` (in the `gates` lane) pins what each kind of change selects. + The full run does its tree-writing steps first (install, the drift gates, one workspace build), then runs the rest as parallel lanes: at most `MO_CI_JOBS` at once, default 8. Each lane's output prints as one block when the lane finishes, so lanes never interleave. The @@ -127,12 +147,13 @@ gate and a Java pom-version drift guard on every push (activate hooks once per clone: `git config core.hooksPath .githooks`). If you drive changes through the no-mistakes validation gate, `.no-mistakes.yaml` pins its -`test` step to `scripts/ci-local.sh --only ts-fast --only ts-unit --strict-toolchains` and -its `lint` step to `scripts/ci-local.sh --only gates --strict-toolchains` — together exactly -`--quick`, split so neither step repeats the other's work. `--strict-toolchains` is part of -both: it promotes a SKIP to a FAIL, so a gate host missing bun cannot report green. Those -commands are read from the **default branch**, so editing them on a feature branch has no -effect until it merges. +`test` step to `scripts/ci-local.sh --affected --strict-toolchains` and its `lint` step to +`scripts/ci-local.sh --only gates --strict-toolchains`: the lanes the change affects, then +the gates every change runs, split so the gates run after the build `test` does. +`--strict-toolchains` is part of both: it promotes a SKIP to a FAIL, so a gate host missing +bun (or, for a change to a port, that port's toolchain) cannot report green. Those commands are +read from the **default branch**, so editing them on a feature branch has no effect until it +merges. ## Releasing diff --git a/scripts/ci-affected-ports.sh b/scripts/ci-affected-ports.sh index 53b2a774e..35db674e6 100755 --- a/scripts/ci-affected-ports.sh +++ b/scripts/ci-affected-ports.sh @@ -3,11 +3,11 @@ # # Usage: scripts/ci-affected-ports.sh # Prints a space-separated subset of: ts java python csharp -# (empty output = docs-only push: run only the cheap shared gates). +# (empty output = a push touching only the root prose files: run just the cheap shared gates). # # Fail-open policy: an unknown base (force-push zeros), an unresolvable -# range, or ANY path not matched by a rule selects ALL ports — this script -# may only ever shrink coverage when it is certain. +# range, or ANY path not matched by a rule in scripts/ci-lane-map.txt selects ALL +# ports — this script may only ever shrink coverage when it is certain. # # NOT the whole selection. local-ci calls `ci-ports-to-run.sh`, which unions this # answer with the ports that are not known-green on main. "What changed" alone @@ -17,25 +17,22 @@ set -uo pipefail ALL="ts java python csharp" +# The path -> lane rules are scripts/ci-lane-map.txt, read through ci-select-lanes.sh, so +# the no-mistakes gate (`ci-local.sh --affected`) and the hosted workflows share ONE table. +# This script keeps its job of answering in PORTS, the unit the workflows' `detect` jobs +# pass on, by mapping each selected lane to its port. +# Docs are NOT prose-only in that table (port tests read docs/), so a docs/ change selects +# every port here too; only the named root prose files select none. +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +MO_SELECT_LIB=1 source "$HERE/ci-select-lanes.sh" + map_paths_to_ports() { # args: changed paths; echoes the port set - local p sel_ts=0 sel_java=0 sel_python=0 sel_csharp=0 - for p in "$@"; do - case "$p" in - server/typescript/*|client/web/*) sel_ts=1 ;; - server/java/*) sel_java=1 ;; - server/python/*) sel_python=1 ;; - server/csharp/*) sel_csharp=1 ;; - fixtures/*|spec/*|scripts/*|.github/*|agent-context/*) - echo "$ALL"; return 0 ;; - docs/*|*.md) : ;; # gates only - *) echo "$ALL"; return 0 ;; # fail open - esac + select_lanes "$@" + local lane port out="" + for lane in $SELECTED_LANES; do + port="$(lane_to_port "$lane")" + case " $out " in *" $port "*) ;; *) out="$out $port" ;; esac done - local out="" - [ "$sel_ts" -eq 1 ] && out="$out ts" - [ "$sel_java" -eq 1 ] && out="$out java" - [ "$sel_python" -eq 1 ] && out="$out python" - [ "$sel_csharp" -eq 1 ] && out="$out csharp" echo "${out# }" } @@ -47,7 +44,9 @@ if [ -z "$BASE" ] || [ "$BASE" = "0000000000000000000000000000000000000000" ] \ || ! git rev-parse -q --verify "$BASE^{commit}" >/dev/null 2>&1; then echo "$ALL"; exit 0 fi -CHANGED="$(git diff --name-only "$BASE" "$HEAD")" || { echo "$ALL"; exit 0; } +# --no-renames: a rename's old path must be listed too, so moving a file OUT of a +# port selects that port exactly like moving one in (matches ci-select-lanes.sh). +CHANGED="$(git diff --no-renames --name-only "$BASE" "$HEAD")" || { echo "$ALL"; exit 0; } [ -z "$CHANGED" ] && { echo ""; exit 0; } # shellcheck disable=SC2086 mapfile -t paths <<<"$CHANGED" diff --git a/scripts/ci-lane-map.txt b/scripts/ci-lane-map.txt new file mode 100644 index 000000000..387908d8b --- /dev/null +++ b/scripts/ci-lane-map.txt @@ -0,0 +1,54 @@ +# Path -> ci-local lane map. THE one table that says which lanes a change must run. +# +# Read by scripts/ci-select-lanes.sh, which two callers share: +# - `scripts/ci-local.sh --affected` (the no-mistakes gate's Test step, .no-mistakes.yaml) +# - scripts/ci-affected-ports.sh (pr-ci.yml and local-ci.yml's `detect` job) +# so the gate and the hosted workflows cannot disagree about what a path affects. +# +# FORMAT: ` | | `, one rule per line. `#` starts a comment. +# a shell glob matched against the repo-relative path; `*` also crosses `/`. +# space-separated lane names, or one of two words: +# ALL every lane below +# NONE no test lane (the `gates` lane still runs; see below) +# Lanes: ts-fast ts-unit java-slow python csharp. These are the ci-local sections a +# change can select. ts-slow, java-fast, cube and mutation are not selectable here: +# the first three need docker or duplicate a lane below, and mutation is diff-scoped +# inside ts-fast already (gate_completeness_diff in ci-local.sh), which this map neither +# widens nor narrows. +# +# RULES, in order. For each changed path the FIRST rule that matches decides it, and a +# change selects the union over its paths. So a rule is a specific-before-general list: +# put a new rule above any broader one that would swallow it. +# +# UNKNOWN MEANS FULL. The last rule matches every path no earlier rule claims and selects +# ALL, so a new top-level directory runs everything until someone decides what it affects. +# The selector also runs ALL on its own when this file is unreadable, a lane name here is +# not a known lane, no base ref resolves, or git cannot list the change. +# +# DOCS ARE NOT PROSE-ONLY here: tests in several ports read files under docs/, so only the +# named root files below are NONE, and any other markdown is unmapped, hence ALL. +# +# THE `gates` LANE IS NOT A ROW. It runs on every change, by its own command: the gate's +# Lint step is `scripts/ci-local.sh --only gates`, and pr-ci.yml's `gates` job is the same. +# It must run AFTER the build the Test step does, which is why it is not folded in there. +# +# Editing this file edits the rule above (`scripts/*` -> ALL), so a change to the map +# runs everything, and scripts/test-ci-lane-selection.sh asserts the map still says so. +# +server/typescript/* | ts-fast ts-unit | the TypeScript server port +client/web/* | ts-fast ts-unit | the universal web client, built and tested by the TypeScript lanes +server/java/* | java-slow | the Java and Kotlin ports (the reactor holds both conformance suites) +server/python/* | python | the Python port +server/csharp/* | csharp | the C# port +fixtures/* | ALL | shared corpora every port runs +spec/* | ALL | the metamodel spec every port implements +scripts/* | ALL | CI scripts and tooling, this map included +.github/* | ALL | the hosted workflows that call the scripts +agent-context/* | ALL | bundled into the TypeScript packages and checked by cross-port gates +docs/* | ALL | port tests read it: Java walks every page for blocks and reads docs/ports, TypeScript and Python read the recipes and docs/features pages, so a page edit is not prose-only +README.md | NONE | root prose; the gates lane checks its claims, examples and test references +CHANGELOG.md | NONE | root prose, same gates +CONTRIBUTING.md | NONE | root prose, same gates +AGENTS.md | NONE | root prose, same gates +CLAUDE.md | NONE | root prose, same gates +* | ALL | unmapped: unknown means a full run diff --git a/scripts/ci-local.sh b/scripts/ci-local.sh index 5cfd84400..692657584 100755 --- a/scripts/ci-local.sh +++ b/scripts/ci-local.sh @@ -73,6 +73,28 @@ # # parallel lane; --quick and # # --no-integration drop it; no other # # section includes it +# scripts/ci-local.sh --affected # AFFECTED-LANE tier: run only the lanes this change needs. +# # Lists the changed paths since the merge base with the +# # base branch (plus uncommitted and untracked files), +# # maps them to lanes with scripts/ci-lane-map.txt, prints +# # which lanes were selected, which were skipped and the +# # rule each path matched, then runs the selected ones as +# # `--only ... --no-integration` would. The +# # selectable lanes are ts-fast, ts-unit, java-slow, +# # python and csharp; a change confined to one port +# # selects that port, anything shared, any path the map +# # does not claim, or no resolvable base selects all of +# # them, and a change to only the root prose files +# # selects none. It never +# # runs `gates`: that lane has its own command +# # (`--only gates`), which the no-mistakes gate runs as +# # its Lint step, after this one's build. +# # Mutually exclusive with --quick, --only and +# # --integration-only. MO_CI_AFFECTED_BASE sets the base +# # ref (default: the mutation gate's, see +# # MO_CI_MUTATION_BASE). MO_CI_FULL=1 forces every +# # selectable lane. The flagless run is unchanged and is +# # what releases use. # scripts/ci-local.sh --no-integration # # Drop the docker/Postgres half of the selected # # sections: the integration suites, the @@ -142,10 +164,11 @@ set -uo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" -QUICK=0; STRICT=0; ONLY=""; NO_INTEG=0; INTEG_ONLY=0 +QUICK=0; STRICT=0; ONLY=""; NO_INTEG=0; INTEG_ONLY=0; AFFECTED=0 while [ $# -gt 0 ]; do case "$1" in --quick) QUICK=1 ;; + --affected) AFFECTED=1 ;; --strict-toolchains) STRICT=1 ;; --no-integration) NO_INTEG=1 ;; --integration-only) INTEG_ONLY=1 ;; @@ -159,6 +182,7 @@ while [ $# -gt 0 ]; do shift done [ -n "$ONLY" ] && [ "$QUICK" -eq 1 ] && { echo "--only and --quick are mutually exclusive" >&2; exit 2; } +[ "$AFFECTED" -eq 1 ] && { [ -n "$ONLY" ] || [ "$QUICK" -eq 1 ] || [ "$INTEG_ONLY" -eq 1 ]; } && { echo "--affected selects the lanes itself; it cannot be combined with --only, --quick or --integration-only" >&2; exit 2; } [ "$INTEG_ONLY" -eq 1 ] && [ "$QUICK" -eq 1 ] && { echo "--integration-only and --quick are mutually exclusive" >&2; exit 2; } [ "$INTEG_ONLY" -eq 1 ] && [ "$NO_INTEG" -eq 1 ] && { echo "--integration-only and --no-integration are mutually exclusive" >&2; exit 2; } MO_CI_JOBS="${MO_CI_JOBS:-8}" @@ -305,12 +329,15 @@ gate_extract_field_kinds() { node scripts/check-extract-field-kinds.mjs; } gate_uv_lock_version() { scripts/check-uv-lock-version.sh; } # ── the lane selector decides what CI runs, so nothing may run it untested ──── +# That now covers three things: the port mapping the hosted workflows use, the lane +# history that widens it, and scripts/ci-lane-map.txt as `--affected` reads it (what the +# no-mistakes gate's Test step runs). # Both selector tests existed-but-unwired at some point: test-ci-affected-ports.sh was # referenced by no lane at all, so the mapping that decides which ports get tested was # itself the one thing nothing tested. That is the worst place in the tree for an unrun # test — a wrong answer here does not fail, it silently stops testing a port. gate_ci_port_selection() { - scripts/test-ci-affected-ports.sh && scripts/test-ci-ports-to-run.sh + scripts/test-ci-affected-ports.sh && scripts/test-ci-ports-to-run.sh && scripts/test-ci-lane-selection.sh } # ── the no-magic-physical-names gate is wired in all five ports ─────────────── @@ -839,18 +866,9 @@ stryker_run() { # stryker_run [stryker args...] — in the metadata package, san return "$rc" } gate_completeness() { bun_install || return 1; stryker_run; } -mutation_base() { # prints the base ref the diff scope compares against; empty when none resolves - local ref best="" best_n="" mb n - if [ -n "${MO_CI_MUTATION_BASE:-}" ]; then echo "$MO_CI_MUTATION_BASE"; return 0; fi - if [ -n "${GITHUB_BASE_REF:-}" ]; then echo "origin/$GITHUB_BASE_REF"; return 0; fi - for ref in origin/main $(git for-each-ref --format='%(refname:short)' 'refs/remotes/origin/release/*'); do - git rev-parse --verify -q "$ref^{commit}" >/dev/null 2>&1 || continue - mb="$(git merge-base HEAD "$ref" 2>/dev/null)" || continue - n="$(git rev-list --count "$mb..HEAD")" - if [ -z "$best_n" ] || [ "$n" -lt "$best_n" ]; then best="$ref"; best_n="$n"; fi - done - echo "$best" -} +# The base-ref rule is shared with --affected and lives in ci-select-lanes.sh (default_base). +MO_SELECT_LIB=1 source "$ROOT/scripts/ci-select-lanes.sh" +mutation_base() { default_base; } gate_completeness_diff() { if [ "${MO_CI_MUTATION_SCOPE:-diff}" = "full" ]; then echo " mutation scope: FULL (MO_CI_MUTATION_SCOPE=full)"; gate_completeness; return @@ -1349,6 +1367,89 @@ lane_kotlin_integ() { lane_jvm_integ kotlin; } # finished csharp lane uses. Every other output goes to a pristine temp copy (--check). lane_showcase() { want gates && run_showcase; return 0; } +# ── --affected: choose the lanes from what changed ──────────────────────────── +# The rules are scripts/ci-lane-map.txt, evaluated by scripts/ci-select-lanes.sh (also what +# the hosted workflows' port detection uses). This only decides ONLY; the lanes then run +# exactly as `--only ... --no-integration` runs them. Every uncertainty widens: no +# resolvable base, a failed git call, an unmapped path all select every selectable lane. +# MO_CI_FULL=1 selects them all on purpose, for a caller that cannot drop the flag. +resolve_affected_lanes() { + local base mb paths why="" lane + if [ "${MO_CI_FULL:-0}" = "1" ]; then + SELECTED_LANES="$CI_SELECTABLE_LANES"; why="MO_CI_FULL=1: the full run was asked for" + else + base="${MO_CI_AFFECTED_BASE:-$(default_base)}" + if [ -z "$base" ] || ! mb="$(git merge-base HEAD "$base" 2>/dev/null)"; then + SELECTED_LANES="$CI_SELECTABLE_LANES" + why="no merge base with '${base:-origin/main or origin/release/*}' resolves here (shallow checkout or no remote refs), so what changed is unknown" + elif ! paths="$(changed_paths "$mb")"; then + SELECTED_LANES="$CI_SELECTABLE_LANES"; why="git could not list the change since $base" + else + local -a list=() + mapfile -t list < <(printf '%s\n' "$paths" | sed '/^$/d') + select_lanes "${list[@]}" + why="${#list[@]} changed path(s) since $base (merge base ${mb:0:12})" + fi + fi + echo "" + echo "── affected-lane selection: $why" + [ -n "${SELECTION_REPORT:-}" ] && echo "$SELECTION_REPORT" + local skipped="" + for lane in $CI_SELECTABLE_LANES; do + case " $SELECTED_LANES " in *" $lane "*) ;; *) skipped="$skipped $lane" ;; esac + done + echo " selected: ${SELECTED_LANES:-(none)}" + if [ -n "$skipped" ]; then echo " skipped: ${skipped# } (not affected by this change)"; else echo " skipped: (none)"; fi + echo " gates: not part of this selection; run them with --only gates. Force every lane with MO_CI_FULL=1." + if [ -z "$SELECTED_LANES" ]; then + echo " nothing to run: no selectable lane is affected." + exit 0 + fi + for lane in $SELECTED_LANES; do ONLY="$ONLY $lane"; done + NO_INTEG=1 +} +if [ "$AFFECTED" -eq 1 ]; then + # MO_CI_SELECTION_PRINT_ONLY=1 stops after the explanation, for the selection test and + # for a reviewer who wants to see what a change would run without running it. + resolve_affected_lanes + if [ "${MO_CI_SELECTION_PRINT_ONLY:-0}" = "1" ]; then exit 0; fi +fi + +# The serial selections' TypeScript steps, in order. The `ts` lane of a parallel --affected +# run calls this too, so the two cannot drift: a lane is these steps, not a copy of them. +run_ts_serial_steps() { + if want_any ts ts-fast; then step_if bun "ts build + typecheck" gate_ts_build_typecheck; fi + if want_any ts ts-fast; then step_if bun "conformance: typescript" gate_conf_ts; fi + if want_any ts ts-unit; then step_if bun "ts unit suites" gate_ts_unit; fi + # The DIFF-scoped mutation step (see gate_completeness_diff); `--only mutation` asks for + # the full pass instead, and gets it in place of this step rather than as well. + if [ -n "$ONLY" ] && want mutation; then + step_if bun "completeness-gate (mutation, full)" gate_completeness + elif want_any ts ts-fast; then + step_if bun "completeness-gate (mutation, changed files)" gate_completeness_diff + fi +} +# One function per other port, shared by the serial heavy tier and the parallel --affected lanes. +run_csharp_steps() { if want csharp; then step_if dotnet "conformance: csharp" gate_conf_csharp; fi; } +run_python_steps() { if want python; then step_if uv "conformance: python" gate_conf_python; fi; } +run_java_reactor_steps() { if want_any java java-slow; then step_if mvn "java-reactor (install)" gate_java_reactor; fi; } + +# ── --affected, several ports: run their lanes side by side ─────────────────── +# Run one after another, a change touching every port would take the SUM of the ports' +# times, slower than the full run's parallel lanes, so more than one selected port group +# runs as lanes exactly as the flagless run does (at most MO_CI_JOBS at once). The groups +# share no files: the ts lane owns node_modules/ and dist/, and java, python and csharp +# use no bun, which is also how pr-ci.yml runs them, as separate jobs. A single selected +# group runs serially, unchanged. TypeScript's two lanes stay one group, in order, because +# ts-unit builds into the dist/ that ts-fast's build writes. +AFFECTED_PARALLEL=0 +if [ "$AFFECTED" -eq 1 ]; then + _groups=0 + in_lane_any ts-fast ts-unit && _groups=$((_groups + 1)) + for _g in java-slow python csharp; do in_lane "$_g" && _groups=$((_groups + 1)); done + [ "$_groups" -gt 1 ] && AFFECTED_PARALLEL=1 +fi + # ── Banner ──────────────────────────────────────────────────────────────────── PARALLEL=0 [ -z "$ONLY" ] && [ "$QUICK" -eq 0 ] && PARALLEL=1 @@ -1359,6 +1460,7 @@ elif [ "$QUICK" -eq 1 ]; then else _mode="full — all ports + reactor + docker, $MO_CI_JOBS lanes at once" fi +[ "$AFFECTED" -eq 1 ] && _mode="affected lanes — $_mode" [ "$NO_INTEG" -eq 1 ] && _mode="$_mode, no integration" [ "$INTEG_ONLY" -eq 1 ] && _mode="$_mode, integration only" echo "metaobjects local CI (mode: $_mode)" @@ -1377,7 +1479,27 @@ if [ "${MO_CI_LIST_ONLY:-0}" = "1" ]; then fi # ── Step invocations ────────────────────────────────────────────────────────── -if [ "$PARALLEL" -eq 0 ]; then +run_or_list_lanes() { + if [ "${MO_CI_LIST_ONLY:-0}" = "1" ]; then + for ((_i = 0; _i < ${#LANE_NAMES[@]}; _i++)); do + echo "── lane ${LANE_NAMES[_i]}${LANE_AFTER[_i]:+ (after ${LANE_AFTER[_i]})}:" + ( "${LANE_FNS[_i]}" ) + done + else + echo ""; echo "── lanes (up to $MO_CI_JOBS at once; each lane's log prints when it finishes) ──" + run_lanes + fi +} + +if [ "$AFFECTED_PARALLEL" -eq 1 ]; then + echo ""; echo "── the selected ports run as parallel lanes ──" + # Longest first, so with fewer slots than lanes the long ones do not queue. + in_lane java-slow && lane java "" run_java_reactor_steps + in_lane python && lane python "" run_python_steps + in_lane csharp && lane csharp "" run_csharp_steps + in_lane_any ts-fast ts-unit && lane ts "" run_ts_serial_steps + run_or_list_lanes +elif [ "$PARALLEL" -eq 0 ]; then # Fast tier: shared gates and TS checks, in their original order. run_gates_offline if want gates; then step_if bun "dependencies installed" bun_install; fi @@ -1387,16 +1509,7 @@ if [ "$PARALLEL" -eq 0 ]; then # completeness on the changed files — the pure-code correctness signal, no docker) and a SLOW lane # (docker/sidecar integration). Isolating integration means a docker/sidecar # hiccup no longer fails the code signal, and vice versa. `--only ts` still runs both. - if want_any ts ts-fast; then step_if bun "ts build + typecheck" gate_ts_build_typecheck; fi - if want_any ts ts-fast; then step_if bun "conformance: typescript" gate_conf_ts; fi - if want_any ts ts-unit; then step_if bun "ts unit suites" gate_ts_unit; fi - # The DIFF-scoped mutation step (see gate_completeness_diff); `--only mutation` asks for - # the full pass instead, and gets it in place of this step rather than as well. - if [ -n "$ONLY" ] && want mutation; then - step_if bun "completeness-gate (mutation, full)" gate_completeness - elif want_any ts ts-fast; then - step_if bun "completeness-gate (mutation, changed files)" gate_completeness_diff - fi + run_ts_serial_steps run_drift_gates if [ "$QUICK" -eq 1 ]; then @@ -1418,11 +1531,11 @@ if [ "$PARALLEL" -eq 0 ]; then # integration). Running them as parallel jobs surfaces the conformance verdict # quickly and means a cancel/OOM mid-integration no longer kills the fast # signal. `--only java` still runs BOTH lanes, in order. - if want csharp; then step_if dotnet "conformance: csharp" gate_conf_csharp; fi + run_csharp_steps if want_any java java-fast; then step_if mvn "conformance: java" gate_conf_java; fi - if want python; then step_if uv "conformance: python" gate_conf_python; fi + run_python_steps if want_any java java-fast; then step_if mvn "conformance: kotlin" gate_conf_kotlin; fi - if want_any java java-slow; then step_if mvn "java-reactor (install)" gate_java_reactor; fi + run_java_reactor_steps # --no-integration drops the docker half; --integration-only keeps ONLY it, which is # why it asks in_lane / in_lane_any (selection) where the checks above ask want. if [ "$NO_INTEG" -eq 1 ]; then @@ -1520,15 +1633,7 @@ else lane cube "" lane_cube lane gates "" lane_gates - if [ "${MO_CI_LIST_ONLY:-0}" = "1" ]; then - for ((_i = 0; _i < ${#LANE_NAMES[@]}; _i++)); do - echo "── lane ${LANE_NAMES[_i]}${LANE_AFTER[_i]:+ (after ${LANE_AFTER[_i]})}:" - ( "${LANE_FNS[_i]}" ) - done - else - echo ""; echo "── lanes (up to $MO_CI_JOBS at once; each lane's log prints when it finishes) ──" - run_lanes - fi + run_or_list_lanes fi # ── Dry-run listing exits before summary ────────────────────────────────────── diff --git a/scripts/ci-select-lanes.sh b/scripts/ci-select-lanes.sh new file mode 100755 index 000000000..6ebcf4a51 --- /dev/null +++ b/scripts/ci-select-lanes.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash +# Select the ci-local lanes a change must run, from scripts/ci-lane-map.txt. +# +# Two callers share this file and the map, so the gate and the hosted workflows cannot +# disagree about what a path affects: +# scripts/ci-local.sh --affected sources it and runs the selected lanes +# scripts/ci-affected-ports.sh sources it and prints the ports those lanes belong to +# +# Run directly it explains a selection, which is how a reviewer sees what a change skips: +# scripts/ci-select-lanes.sh [] # the diff against the merge base with +# git diff --name-only main | scripts/ci-select-lanes.sh - # paths on stdin +# It prints the rule each path matched and a final `lanes:` line. With no base it uses the +# one the gate uses: MO_CI_AFFECTED_BASE, else default_base (below), else origin/main. +# +# Fail-open policy, the one rule that matters: this may only shrink what runs when it is +# certain. An unreadable map, an unknown lane name, an unresolvable base, a git failure and +# any path no rule claims all select every lane. "Unknown" never means "skip". +set -uo pipefail + +CI_SELECT_HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +CI_LANE_MAP="${MO_CI_LANE_MAP:-$CI_SELECT_HERE/ci-lane-map.txt}" + +# The lanes a change can select, in the order they run. The map's `ALL` means exactly this. +CI_SELECTABLE_LANES="ts-fast ts-unit java-slow python csharp" + +# The port each lane belongs to, for callers that think in ports (the hosted workflows). +lane_to_port() { + case "$1" in + ts-fast|ts-unit) echo ts ;; + java-slow) echo java ;; + python) echo python ;; + csharp) echo csharp ;; + esac +} + +# _trim : sets to without surrounding whitespace (no subshell). +_trim() { local s="$2"; s="${s#"${s%%[![:space:]]*}"}"; printf -v "$1" %s "${s%"${s##*[![:space:]]}"}"; } + +# Prints one `\t\t` line per rule, comments and blanks dropped. +# Returns 1 when the map cannot be read. +lane_map_rules() { + [ -r "$CI_LANE_MAP" ] || return 1 + local line pat lanes why + while IFS= read -r line || [ -n "$line" ]; do + case "$line" in ''|'#'*|[[:space:]]*'#'*) continue ;; esac + _trim line "$line"; [ -n "$line" ] || continue + IFS='|' read -r pat lanes why <<<"$line" + _trim pat "$pat"; _trim lanes "$lanes"; _trim why "${why:-}" + printf '%s\t%s\t%s\n' "$pat" "$lanes" "$why" + done < "$CI_LANE_MAP" +} + +# lane_map_problems []: prints a problem per line when the map is malformed; empty +# output = valid. A caller that already read the rules passes them, so the map is read once. +lane_map_problems() { + local rules pat lanes why l last="" + if [ "$#" -gt 0 ]; then rules="$1"; else rules="$(lane_map_rules)" || { echo "cannot read $CI_LANE_MAP"; return 0; }; fi + [ -n "$rules" ] || { echo "$CI_LANE_MAP has no rules"; return 0; } + while IFS=$'\t' read -r pat lanes why; do + [ -n "$pat" ] || echo "a rule has an empty glob" + [ -n "$lanes" ] || echo "rule '$pat' names no lanes (write NONE for none)" + [ -n "$why" ] || echo "rule '$pat' has no reason" + for l in $lanes; do + case " ALL NONE $CI_SELECTABLE_LANES " in + *" $l "*) ;; + *) echo "rule '$pat' names '$l', which is not a selectable lane" ;; + esac + done + last="$pat" + done <<<"$rules" + [ "$last" = "*" ] || echo "the last rule must be '*' (unmapped -> ALL), found '$last'" +} + +# select_lanes ... +# Sets SELECTED_LANES (space-separated, run order; empty = no test lane) and +# SELECTION_REPORT (one line per rule that matched, naming its paths). +select_lanes() { + SELECTED_LANES=""; SELECTION_REPORT="" + local want=" " rules l + + if [ "$#" -eq 0 ]; then + SELECTION_REPORT=" no changed paths: nothing to run" + return 0 + fi + + local problems + if ! rules="$(lane_map_rules)"; then + problems="cannot read $CI_LANE_MAP" + else + problems="$(lane_map_problems "$rules")" + fi + if [ -n "$problems" ]; then + SELECTED_LANES="$CI_SELECTABLE_LANES" + SELECTION_REPORT=" the lane map is unreadable or invalid, so every lane runs: $(printf '%s\n' "$problems" | paste -sd';' -)" + return 0 + fi + + local -a pats=() lanes_of=() why_of=() + local pat lanes why + while IFS=$'\t' read -r pat lanes why; do + pats+=("$pat"); lanes_of+=("$lanes"); why_of+=("$why") + done <<<"$rules" + + local -A paths_of=() count_of=() + local p i hit + for p in "$@"; do + hit=-1 + for i in "${!pats[@]}"; do + # shellcheck disable=SC2254 # the rule's glob is meant to be a pattern, not a literal + case "$p" in ${pats[$i]}) hit="$i"; break ;; esac + done + if [ "$hit" -lt 0 ]; then # unreachable while the last rule is '*', kept as the backstop + want=" $CI_SELECTABLE_LANES " + SELECTION_REPORT+=" ALL <- (no rule): $p"$'\n' + continue + fi + count_of[$hit]=$(( ${count_of[$hit]:-0} + 1 )) + [ "${count_of[$hit]}" -le 4 ] && paths_of[$hit]+="${paths_of[$hit]:+, }$p" + case " ${lanes_of[$hit]} " in + *" ALL "*) want=" $CI_SELECTABLE_LANES " ;; + *" NONE "*) ;; + *) want+="${lanes_of[$hit]} " ;; + esac + done + + for i in "${!pats[@]}"; do + [ -n "${count_of[$i]:-}" ] || continue + local more=""; [ "${count_of[$i]}" -gt 4 ] && more=", and $(( count_of[$i] - 4 )) more" + SELECTION_REPORT+=" ${lanes_of[$i]} <- ${pats[$i]} (${why_of[$i]}): ${paths_of[$i]}$more"$'\n' + done + SELECTION_REPORT="${SELECTION_REPORT%$'\n'}" + + for l in $CI_SELECTABLE_LANES; do + case "$want" in *" $l "*) SELECTED_LANES+="${SELECTED_LANES:+ }$l" ;; esac + done +} + +# default_base — the base ref a change is measured against, empty when none resolves. The one +# definition: the gate's mutation scope and --affected both call it, so they cannot disagree. +# MO_CI_MUTATION_BASE and GITHUB_BASE_REF name it outright; otherwise it is whichever of +# origin/main and origin/release/* is closest to HEAD (a release-branch change has main as a +# distant ancestor). +default_base() { + local ref best="" best_n="" mb n + if [ -n "${MO_CI_MUTATION_BASE:-}" ]; then echo "$MO_CI_MUTATION_BASE"; return 0; fi + if [ -n "${GITHUB_BASE_REF:-}" ]; then echo "origin/$GITHUB_BASE_REF"; return 0; fi + for ref in origin/main $(git for-each-ref --format='%(refname:short)' 'refs/remotes/origin/release/*'); do + git rev-parse --verify -q "$ref^{commit}" >/dev/null 2>&1 || continue + mb="$(git merge-base HEAD "$ref" 2>/dev/null)" || continue + n="$(git rev-list --count "$mb..HEAD")" + if [ -z "$best_n" ] || [ "$n" -lt "$best_n" ]; then best="$ref"; best_n="$n"; fi + done + echo "$best" +} + +# changed_paths — the paths this change touches, one per line. +# Committed work since the merge base, plus uncommitted and untracked files, so a run in a +# dirty tree selects for what it will actually test. --no-renames lists a rename's old path +# too: moving a file OUT of a port affects that port as much as moving one in. +changed_paths() { + local mb="$1" tracked untracked + tracked="$(git diff --no-renames --name-only "$mb")" || return 1 + untracked="$(git ls-files --others --exclude-standard)" || return 1 + printf '%s\n%s\n' "$tracked" "$untracked" | sed '/^$/d' | sort -u +} + +# Test seam: `MO_SELECT_LIB=1 source` exposes the functions and does nothing else. +[ "${MO_SELECT_LIB:-0}" = "1" ] && return 0 + +# ── CLI: explain a selection ───────────────────────────────────────────────── +BASE_ARG="${1:-${MO_CI_AFFECTED_BASE:-$(default_base)}}" +BASE_ARG="${BASE_ARG:-origin/main}" +if [ "$BASE_ARG" = "-" ]; then + _list="$(cat)" +else + _mb="$(git merge-base HEAD "$BASE_ARG" 2>/dev/null)" || { echo "no merge base with '$BASE_ARG': every lane runs" >&2; echo "lanes: $CI_SELECTABLE_LANES"; exit 0; } + _list="$(changed_paths "$_mb")" || { echo "git could not list the change: every lane runs" >&2; echo "lanes: $CI_SELECTABLE_LANES"; exit 0; } +fi +mapfile -t _paths < <(printf '%s\n' "$_list" | sed '/^$/d') +select_lanes "${_paths[@]}" +echo "$SELECTION_REPORT" +echo "lanes: $SELECTED_LANES" diff --git a/scripts/test-ci-affected-ports.sh b/scripts/test-ci-affected-ports.sh index ae3ab8521..792bb13fd 100755 --- a/scripts/test-ci-affected-ports.sh +++ b/scripts/test-ci-affected-ports.sh @@ -29,7 +29,8 @@ check "ts java python csharp" "scripts/ci-local.sh" check "ts java python csharp" ".github/workflows/local-ci.yml" check "ts java python csharp" "agent-context/skills/x.md" check "ts java python csharp" "weird-new-toplevel/file.txt" # unmapped -> fail open -check "" "docs/features/cli.md" # docs-only -> gates only +check "ts java python csharp" "docs/recipes/generators/python/json_schema.py" # executed by port tests -> not docs +check "ts java python csharp" "docs/features/cli.md" # port tests read docs/ -> all check "" "README.md" "CHANGELOG.md" # md-only -> gates only check "ts python" "server/typescript/a.ts" "server/python/b.py" check "ts java python csharp" "docs/x.md" "fixtures/y.json" # mixed: fixtures wins diff --git a/scripts/test-ci-lane-selection.sh b/scripts/test-ci-lane-selection.sh new file mode 100755 index 000000000..cbbe530d7 --- /dev/null +++ b/scripts/test-ci-lane-selection.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +# Test of the affected-lane selection: scripts/ci-lane-map.txt as read by +# scripts/ci-select-lanes.sh, and `scripts/ci-local.sh --affected` end to end. +# +# Part 1 feeds fixed change sets straight into select_lanes and asserts the lanes each +# selects: one port, two ports, the shared model, a CI script, an unmapped path, docs, and +# an empty diff. Part 2 asserts the map itself is well formed and that the rules which +# must stay (a change to the map or a script runs everything) have not been edited away. +# Part 3 builds a throwaway repository and runs the real `ci-local.sh --affected` in it, +# so the git side (merge base, committed + uncommitted + untracked, fail-open) is exercised +# and not just the pure function. Nothing here runs a lane. +set -uo pipefail +cd "$(dirname "$0")/.." || exit 1 +MO_SELECT_LIB=1 source scripts/ci-select-lanes.sh + +fails=0 +check() { # check "" "" "" + if [ "$2" != "$1" ]; then + echo "FAIL: $3 -> '$2' (expected '$1')" >&2; fails=$((fails+1)) + else + echo "ok: $3 -> '${2:-}'" + fi +} +lanes() { select_lanes "$@"; echo "$SELECTED_LANES"; } + +ALL="ts-fast ts-unit java-slow python csharp" + +# ── 1. change sets ──────────────────────────────────────────────────────────── +check "ts-fast ts-unit" "$(lanes server/typescript/packages/cli/src/x.ts)" "one port: typescript" +check "ts-fast ts-unit" "$(lanes client/web/packages/react/src/y.tsx)" "one port: the web client is typescript's" +check "java-slow" "$(lanes server/java/metadata/src/main/java/A.java server/java/codegen-kotlin/src/main/kotlin/B.kt)" "one port: java and kotlin" +check "python" "$(lanes server/python/src/metaobjects/loader.py)" "one port: python" +check "csharp" "$(lanes server/csharp/MetaObjects/Loader.cs)" "one port: csharp" +check "ts-fast ts-unit python" "$(lanes server/python/b.py server/typescript/a.ts)" "two ports, given out of order, run in lane order" +check "ts-fast ts-unit java-slow python csharp" "$(lanes server/csharp/a.cs server/java/b.java server/python/c.py server/typescript/d.ts)" "all four ports touched" +check "$ALL" "$(lanes fixtures/conformance/foo/meta.json)" "shared fixtures run everything" +check "$ALL" "$(lanes spec/metamodel.md)" "the spec runs everything" +check "$ALL" "$(lanes agent-context/skills/x.md)" "agent context runs everything" +check "$ALL" "$(lanes scripts/ci-local.sh)" "a CI script runs everything" +check "$ALL" "$(lanes scripts/ci-lane-map.txt)" "the map itself runs everything" +check "$ALL" "$(lanes .github/workflows/local-ci.yml)" "a workflow runs everything" +check "$ALL" "$(lanes weird-new-toplevel/file.txt)" "an unmapped path runs everything" +check "$ALL" "$(lanes .no-mistakes.yaml)" "an unmapped dotfile runs everything" +check "$ALL" "$(lanes server/python/a.py weird-new-toplevel/file.txt)" "one unmapped path among mapped ones runs everything" +check "$ALL" "$(lanes docs/x.md fixtures/y.json)" "docs beside a shared fixture: the fixture wins" +check "$ALL" "$(lanes docs/recipes/generators/typescript/x.ts)" "recipe generators are executed by port tests, so they are not docs" +check "$ALL" "$(lanes docs/features/cli.md)" "a docs page: port tests read docs/, so it runs everything" +check "$ALL" "$(lanes docs/ports/java.md)" "the java port doc is read by a java test" +check "$ALL" "$(lanes examples/foo/README.md)" "markdown outside the named root files is unmapped" +check "" "$(lanes README.md CHANGELOG.md CONTRIBUTING.md)" "root prose only: no test lane, the gates lane checks it" +check "python" "$(lanes server/python/README.md)" "a port's own markdown still selects the port" +check "" "$(lanes)" "empty diff: no test lane" + +select_lanes; check " no changed paths: nothing to run" "$SELECTION_REPORT" "empty diff says why" +select_lanes server/java/A.java server/java/B.java server/java/C.java server/java/D.java server/java/E.java server/java/F.java +case "$SELECTION_REPORT" in + *"java-slow <- server/java/*"*"server/java/A.java"*"and 2 more"*) echo "ok: the report names the rule, the paths and how many it elided" ;; + *) echo "FAIL: report does not name rule, paths and the elided count: $SELECTION_REPORT" >&2; fails=$((fails+1)) ;; +esac + +# ports, for the hosted workflows' detect jobs: derived from the same lanes +check "ts python" "$(MO_AFFECTED_TEST=1 bash -c 'source scripts/ci-affected-ports.sh; map_paths_to_ports server/typescript/a.ts server/python/b.py')" "ports derive from lanes" + +# ── 2. the map ──────────────────────────────────────────────────────────────── +check "" "$(lane_map_problems)" "the committed map is well formed" + +bad="$(mktemp "${DEV_SCRATCH:-${TMPDIR:-/tmp}}/ci-lane-map-bad.XXXXXX")" +printf '%s\n' 'server/python/* | pyton | typo in a lane name' 'weird/* | ALL | no catch-all follows' > "$bad" +check "yes" "$([ -n "$(CI_LANE_MAP="$bad" lane_map_problems)" ] && echo yes || echo no)" "a misspelt lane and a missing catch-all are reported" +# A map that is broken must widen, never narrow. +check "$ALL" "$(CI_LANE_MAP="$bad" lanes server/python/a.py)" "an invalid map selects every lane" +check "$ALL" "$(CI_LANE_MAP=/nonexistent lanes server/python/a.py)" "an unreadable map selects every lane" +rm -f "$bad" + +# ── 3. ci-local.sh --affected on a real repository ──────────────────────────── +tmp="$(mktemp -d "${DEV_SCRATCH:-${TMPDIR:-/tmp}}/ci-lane-selection.XXXXXX")" +trap 'rm -rf "$tmp"' EXIT +git init -q -b main "$tmp/repo" || { echo "FAIL: cannot create a throwaway repository" >&2; exit 1; } +mkdir -p "$tmp/repo/scripts" "$tmp/repo/server/python" "$tmp/repo/server/typescript" "$tmp/repo/docs" +cp scripts/ci-local.sh scripts/ci-select-lanes.sh scripts/ci-lane-map.txt scripts/ci-affected-ports.sh "$tmp/repo/scripts/" +echo a > "$tmp/repo/server/python/a.py"; echo a > "$tmp/repo/server/typescript/a.ts"; echo a > "$tmp/repo/docs/a.md" +git -C "$tmp/repo" add -A +git -C "$tmp/repo" -c user.name=t -c user.email=t@example.invalid commit -q -m base +git -C "$tmp/repo" checkout -q -b change + +run() { # run [ENV=val ...] — prints the selected line of the explanation + ( cd "$tmp/repo" && env MO_CI_AFFECTED_BASE=main MO_CI_SELECTION_PRINT_ONLY=1 "$@" scripts/ci-local.sh --affected 2>&1 ) \ + | sed -n 's/^ selected: //p' +} +commit() { git -C "$tmp/repo" add -A; git -C "$tmp/repo" -c user.name=t -c user.email=t@example.invalid commit -q -m "$1"; } + +check "(none)" "$(run)" "git: no change selects nothing" +echo b >> "$tmp/repo/server/python/a.py"; commit python +check "python" "$(run)" "git: a committed python change selects python" +echo b >> "$tmp/repo/server/typescript/a.ts" +check "ts-fast ts-unit python" "$(run)" "git: an uncommitted typescript change joins it" +commit typescript +echo b >> "$tmp/repo/README.md" +check "ts-fast ts-unit python" "$(run)" "git: an untracked root-prose file adds no lane" +commit prose +check "$ALL" "$(run MO_CI_FULL=1)" "MO_CI_FULL=1 selects every lane" +check "$ALL" "$(run MO_CI_AFFECTED_BASE=no-such-ref)" "an unresolvable base selects every lane" +# How the selection RUNS (MO_CI_LIST_ONLY prints the plan): several port groups are parallel +# lanes, so a change to every port is not slower than the full run; one group stays serial. +plan() { ( cd "$tmp/repo" && env MO_CI_AFFECTED_BASE=main MO_CI_LIST_ONLY=1 "$@" scripts/ci-local.sh --affected 2>&1 ); } +lane_blocks() { { grep -o '^── lane [a-z]*' || true; } | sed 's/^── lane //' | sort | paste -sd' ' -; } +check "csharp java python ts" "$(plan MO_CI_FULL=1 | lane_blocks)" "every port selected: one parallel lane per port group" +check "python ts" "$(plan | lane_blocks)" "python and typescript selected: two parallel lanes" +check "yes" "$(plan | grep 'run as parallel lanes' >/dev/null && echo yes || echo no)" "several groups are announced as parallel" +check "" "$(plan MO_CI_AFFECTED_BASE=HEAD~2 | lane_blocks)" "one group (typescript, the docs adds none) stays serial" +check "yes" "$(plan MO_CI_AFFECTED_BASE=HEAD~2 | grep 'ts build + typecheck' >/dev/null && echo yes || echo no)" "the serial plan still lists typescript's steps" +# A rename's OLD path counts too (ci-affected-ports.sh diffs with --no-renames, like +# changed_paths does): moving a file OUT of a path that maps broader than its destination +# must select what the old path affects, not only the new one. Before --no-renames this +# printed 'java', silently dropping the ts/python/csharp lanes agent-context selects. +# (Before the `map` corruption below, which widens every later selection to ALL on purpose.) +mkdir -p "$tmp/repo/agent-context/skills" "$tmp/repo/server/java" +echo s > "$tmp/repo/agent-context/skills/x.md"; commit shared +git -C "$tmp/repo" mv agent-context/skills/x.md server/java/x.md; commit rename-into-java +ports() { ( cd "$tmp/repo" && scripts/ci-affected-ports.sh "$(git rev-parse HEAD~1)" HEAD ); } +check "ts java python csharp" "$(ports)" "git: a rename out of agent-context still selects every port" +git -C "$tmp/repo" mv server/java/x.md server/python/x.py; commit rename-java-to-python +check "java python" "$(ports)" "git: a rename between ports selects both" + +echo b >> "$tmp/repo/scripts/ci-lane-map.txt"; commit map +check "$ALL" "$(run)" "git: editing the map selects every lane" +# --affected is a selector; combining it with the other selectors is refused, not guessed at. +( cd "$tmp/repo" && scripts/ci-local.sh --affected --only gates >/dev/null 2>&1 ); check "2" "$?" "--affected with --only is a usage error" +( cd "$tmp/repo" && scripts/ci-local.sh --affected --quick >/dev/null 2>&1 ); check "2" "$?" "--affected with --quick is a usage error" + +[ "$fails" -eq 0 ] && echo "ALL PASS" || { echo "$fails failure(s)" >&2; exit 1; }