~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
ci(npm): compute branch slug in discover, fail loud on collisions (#5187)
LearningCircuit86dbe94f8ff4
diff --git a/.github/SECURITY_SCORECARD.md b/.github/SECURITY_SCORECARD.mdindex ae29c1b9d1..6e9d1d2cdb 100644--- a/.github/SECURITY_SCORECARD.md+++ b/.github/SECURITY_SCORECARD.md@@ -121,7 +121,7 @@ The 3 "unpinned" npm commands are operational commands, not package installation|------|------|---------|-------------------|| npm-audit.yml | 56 | `npm i --package-lock-only` | Generates lockfile only || npm-audit.yml | 72 | `npm i --package-lock-only` | Generates lockfile only |-| update-npm-dependencies.yml | 74 | `npm update` | Intentionally updates to latest |+| update-npm-dependencies.yml | "👚 Update to latest compatible versions (lockfile only)" step | `npm update` | Intentionally updates to latest |These commands don't install packages directly - they either generate lockfilesor intentionally update packages. They cannot and should not be "pinned".diff --git a/.github/workflows/update-npm-dependencies.yml b/.github/workflows/update-npm-dependencies.ymlindex 4df9c382e1..7a2b81f432 100644--- a/.github/workflows/update-npm-dependencies.yml+++ b/.github/workflows/update-npm-dependencies.yml@@ -35,6 +35,15 @@ jobs:# unrecognized package enter the matrix with no build/test is the# exact coverage gap this job exists to prevent; add explicit# classification above before such a dir can be swept.+ # - every entry also carries a "slug": the per-directory PR-branch+ # suffix ("." -> "root", "/" -> "-"). Slugs outside the conservative+ # ASCII policy, slugs that would make an invalid Git ref, and collisions+ # between two dirs' slugs fail this job (see the guards below).+ # - root-level dot-dirs (./.git, ./.venv, ./.claude, ./.github) are+ # pruned entirely (see below), so a lockfile under a NEW root+ # dot-dir is silently exempt from the fail-loud guard above —+ # deliberate trade-off from PR #5090, revisit if a dot-dir ever+ # legitimately carries an npm project.discover:name: 🔍 Discover lockfile dirsruns-on: ubuntu-latest@@ -57,25 +66,70 @@ jobs:# jq is preinstalled on ubuntu-latest; no install step needed.# -prune avoids descending into excluded trees (filtering with# -not -path would still traverse them). './.*' prunes every- # root-level dot-dir (.git, .github, .claude, and a local .venv —- # site-packages can ship npm lockfiles, which would otherwise trip+ # root-level dot-dir (.git, .github, .claude, and a local .venv — a+ # .venv can contain npm lockfiles generated at runtime by packages'+ # own node bootstraps, e.g. readabilipy, which would otherwise trip# the unknown-dir guard when the test suite runs this script on a# dev checkout).- name: List package-lock.json dirsid: listrun: |set -euo pipefail- MATRIX=$(find . \+ # Keep discovery NUL-delimited until every path has been JSON+ # encoded. Git permits newlines in filenames; a line-delimited+ # find|sed pipeline can split one real directory into multiple fake+ # matrix entries and let the invalid-slug guard inspect the wrong+ # values. The temporary files are created safely and removed on exit.+ LOCKFILE_DIRS_FILE=$(mktemp)+ PATHS_JSON_FILE=$(mktemp)+ trap 'rm -f "$LOCKFILE_DIRS_FILE" "$PATHS_JSON_FILE"' EXIT+ find . \\( -path '*/node_modules' -o -path './.*' \) -prune \- -o -name package-lock.json -printf '%h\n' \- | sed 's|^\./||' \- | sort -u \- | jq -R '{path: ., build_cmd: (if . == "." then "npm run build" else "" end), test_cmd: (if (. == "tests" or startswith("tests/")) then "echo \"Skipping tests in CI - requires server\"" else "" end), cache_path: "package-lock.json"}' \- | jq -s -c .)- if [ "$(echo "$MATRIX" | jq 'length')" -eq 0 ]; then+ -o -name package-lock.json -printf '%h\0' \+ > "$LOCKFILE_DIRS_FILE"+ while IFS= read -r -d '' dir; do+ jq -cn --arg path "${dir#./}" '$path'+ done < "$LOCKFILE_DIRS_FILE" > "$PATHS_JSON_FILE"+ MATRIX=$(jq -s -c '+ unique+ | map({+ path: .,+ slug: (if . == "." then "root" else gsub("/"; "-") end),+ build_cmd: (if . == "." then "npm run build" else "" end),+ test_cmd: (if (. == "tests" or startswith("tests/")) then "echo \"Skipping tests in CI - requires server\"" else "" end),+ cache_path: "package-lock.json"+ })+ ' "$PATHS_JSON_FILE")+ if [ "$(echo "$MATRIX" | jq 'length')" = "0" ]; thenecho "::error::No package-lock.json files discovered — matrix would be empty"exit 1fi+ # Reject slugs that would make create-pull-request fail when it+ # creates update-npm-dependencies-${run_number}-${slug}. The ASCII+ # charset is intentionally stricter than Git: unusual directory+ # names should be renamed instead of widening the branch policy.+ # The remaining checks cover Git-invalid values that pass the+ # charset: "..", a trailing ".", and a ".lock" suffix. The 200-byte+ # ceiling also leaves room within the common 255-byte ref-component+ # limit for the fixed prefix, run number, and Git's temporary+ # ".lock" suffix.+ MAX_BRANCH_SLUG_LENGTH=200+ INVALID_SLUGS=$(echo "$MATRIX" | jq -c \+ --argjson max_slug_length "$MAX_BRANCH_SLUG_LENGTH" '[+ .[] | select(+ (.slug | length == 0)+ or (.slug | test("[^A-Za-z0-9._-]"))+ or (.slug | contains(".."))+ or (.slug | endswith("."))+ or (.slug | endswith(".lock"))+ or (.slug | length > $max_slug_length)+ )+ ]')+ if [ "$(echo "$INVALID_SLUGS" | jq 'length')" != "0" ]; then+ echo "::error::Invalid branch slug(s) — rename these lockfile directories:"+ echo "$INVALID_SLUGS" | jq -r '.[] | " - path=" + (.path | @json) + ", slug=" + (.slug | @json)'+ exit 1+ fi# Fail loud on lockfile dirs that are neither the repo root nor# under tests/: silently skipping their build/test is the coverage# gap this job prevents. Promote such a dir to explicit commands.@@ -85,6 +139,19 @@ jobs:while IFS= read -r d; do echo " - $d"; done <<< "$UNKNOWN"exit 1fi+ # Slugs must be unique — each maps to a PR branch name. A collision+ # (e.g. tests/a-b vs tests/a/b, both -> tests-a-b) would put two+ # legs on one branch, reintroducing the shared-ref race fixed in+ # PR #5090. Fail loud so the colliding dir gets renamed instead.+ DUPES=$(echo "$MATRIX" | jq -r '[.[].slug] | group_by(.) | map(select(length > 1) | .[0]) | .[]')+ if [ -n "$DUPES" ]; then+ echo "::error::Colliding branch slugs — these dirs would share a PR branch:"+ while IFS= read -r s; do+ echo " slug '$s' from:"+ echo "$MATRIX" | jq -r --arg s "$s" '.[] | select(.slug == $s) | " - " + .path'+ done <<< "$DUPES"+ exit 1+ fiecho "matrix=$MATRIX" >> "$GITHUB_OUTPUT"echo "Discovered $(echo "$MATRIX" | jq 'length') dir(s):"echo "$MATRIX" | jq -r '.[].path'@@ -98,7 +165,8 @@ jobs:pull-requests: write # Required for creating PRsstrategy:# Legs are independent per-directory sweeps: one dir's failure must not- # cancel the others (fail-fast cancelled 5 of 8 legs in run #36).+ # cancel the others (fail-fast cancelled 5 of 8 legs in run #36,+ # PR #5090).fail-fast: falsematrix:npm_directory: ${{ fromJson(needs.discover.outputs.matrix) }}@@ -198,12 +266,6 @@ jobs:# Use $NPM_DIR (env) rather than a direct GitHub-Actions# expression so a path can never be expanded as a shell expr.echo "path=$NPM_DIR"- # Branch-name slug: "." -> "root", "/" -> "-". Every leg MUST- # get its own branch — with a shared name, concurrent legs race- # on the same ref (run #36 failed on "Reference cannot be- # updated") and sequential legs force-overwrite each other's- # commits, silently dropping updates.- echo "slug=$(echo "$NPM_DIR" | sed 's|^\.$|root|; s|/|-|g')"} >> "$GITHUB_OUTPUT"fi@@ -211,7 +273,7 @@ jobs:uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1if: steps.verify-changed-files.outputs.changed == 'true'with:- branch: update-npm-dependencies-${{ github.run_number }}-${{ steps.verify-changed-files.outputs.slug }}+ branch: update-npm-dependencies-${{ github.run_number }}-${{ matrix.npm_directory.slug }}title: 🤖 Update dependencies (${{ steps.verify-changed-files.outputs.path }})body: |This PR updates dependencies to their latest compatible versions.@@ -228,7 +290,7 @@ jobs:add-paths: |${{ steps.verify-changed-files.outputs.path }}/package.json${{ steps.verify-changed-files.outputs.path }}/package-lock.json- commit-message: 🤖 Update dependencies+ commit-message: 🤖 Update dependencies (${{ steps.verify-changed-files.outputs.path }})labels: maintenancedraft: falsebase: maindiff --git a/changelog.d/+npm-update-slug-in-discover.misc.md b/changelog.d/+npm-update-slug-in-discover.misc.mdnew file mode 100644index 0000000000..4619901231--- /dev/null+++ b/changelog.d/+npm-update-slug-in-discover.misc.md@@ -0,0 +1 @@+The npm-update workflow's per-directory PR-branch slug (`"." -> "root"`, `/` -> `-`) is now computed once, in the `discover` job's jq, and threaded through the matrix as `matrix.npm_directory.slug` — the `update` job's create-pull-request step consumes it directly instead of re-deriving it in a separate `verify-changed-files` step. Discovery now preserves paths with NUL delimiters until JSON encoding and fails loud if a slug violates the conservative branch-name policy, exceeds the safe length limit, or collides with another lockfile directory, naming the offenders and closing latent gaps that otherwise fail late in PR creation or let two legs silently share one PR branch. Commit messages now name the updated directory, matching the PR title.diff --git a/tests/ci/test_npm_update_matrix.py b/tests/ci/test_npm_update_matrix.pyindex 22cb9972ae..0870fa79d0 100644--- a/tests/ci/test_npm_update_matrix.py+++ b/tests/ci/test_npm_update_matrix.py@@ -3,23 +3,30 @@Coverage in two layers:1. Structural ("guardian") tests parse the workflow YAML and pin the wiring- that makes the matrix dynamic: the ``discover`` job, its ``matrix`` output,- the ``update`` job consuming ``fromJson(needs.discover.outputs.matrix)``,- the ``set -euo pipefail`` and empty-matrix guard, and the minimal- ``contents: read`` permission. These prevent the safety features from being- silently removed — same philosophy as+ that makes the matrix dynamic: the ``discover`` job, its ``matrix``+ output, the ``update`` job consuming+ ``fromJson(needs.discover.outputs.matrix)``, the ``set -euo pipefail``+ and empty-matrix guard, the minimal ``contents: read`` permission,+ ``fail-fast: false`` on the per-directory legs, the create-pull-request+ branch template's well-formed ``${{ matrix.npm_directory.slug }}``+ expression (the slug is computed once in discover's jq and threaded+ through the matrix — it is no longer re-derived in the update job), and+ the fail-loud invalid-slug and slug-collision guards in discover. These+ prevent the safety features from being silently removed — same philosophy as``tests/ci/test_release_gate_integrity.py``.2. Behavioral tests extract the discover step's real ``run:`` script from theYAML and execute it against synthetic fixture trees via subprocess. Thisproves the find|jq pipeline produces a correct matrix (repo root builds,- ``tests/*`` dirs skip, excluded dirs ignored) and that the empty-matrix- guard fails the job — without duplicating the pipeline, so it cannot drift- from the workflow.+ ``tests/*`` dirs skip, excluded dirs ignored, a "slug" computed per entry)+ and that the empty-matrix, unknown-path, invalid-slug, and slug-collision+ guards all fail the job loudly — without duplicating the pipeline, so it+ cannot drift from the workflow."""import jsonimport os+import reimport shutilimport subprocessimport sys@@ -30,6 +37,7 @@ import yamlREPO_ROOT = Path(__file__).resolve().parents[2]WORKFLOW = REPO_ROOT / ".github" / "workflows" / "update-npm-dependencies.yml"+_MAX_BRANCH_SLUG_LENGTH = 200def _gnu_find_available() -> bool:@@ -83,33 +91,6 @@ def _discover_run(workflow: dict) -> str:)-def _slug_line(workflow: dict) -> str:- """Return the ``echo "slug=..."`` line from verify-changed-files."""- for step in workflow["jobs"]["update"]["steps"]:- if step.get("id") == "verify-changed-files":- for line in step["run"].splitlines():- if line.strip().startswith('echo "slug='):- return line.strip()- raise AssertionError(- "update job's verify-changed-files step emits no slug= line — "- "cannot exercise the branch-slug transform"- )---def _slug_for(slug_line: str, npm_dir: str) -> str:- """Execute the real slug line with NPM_DIR set; return the slug."""- proc = subprocess.run(- ["bash", "-euo", "pipefail", "-c", slug_line],- env={**os.environ, "NPM_DIR": npm_dir},- capture_output=True,- text=True,- )- assert proc.returncode == 0, proc.stderr- out = proc.stdout.strip()- assert out.startswith("slug="), out- return out[len("slug=") :]--# ---------------------------------------------------------------------------# Structural ("guardian") tests — pin the wiring, prevent silent removal# ---------------------------------------------------------------------------@@ -157,7 +138,16 @@ class TestWorkflowStructure:def test_discover_has_empty_matrix_guard(self, workflow):run = _discover_run(workflow)- assert "-eq 0" in run and "exit 1" in run, (+ # Pin the error message, not the comparison operator: the guard was+ # hardened from a numeric "-eq 0"/"-ne 0" jq-length comparison (which+ # `[` can error on for a non-integer substitution, silently skipping+ # the guard) to a string comparison against "0", and both of+ # discover's zero-length guards now contain that same `= "0"` shape+ # — so pinning it here would stop distinguishing this guard from the+ # invalid-slug guard below.+ assert "No package-lock.json files discovered" in run and (+ "exit 1" in run+ ), ("The empty-matrix guard is gone — if find returns nothing, the ""update job would silently no-op instead of failing loudly.")@@ -171,6 +161,36 @@ class TestWorkflowStructure:"to prevent.")+ def test_discover_has_slug_collision_guard(self, workflow):+ run = _discover_run(workflow)+ assert "Colliding branch slugs" in run, (+ "The slug-collision guard is gone from discover's list step — "+ "two directories whose paths map to the same branch slug (e.g. "+ "tests/a-b and tests/a/b, both -> tests-a-b) would silently "+ "share one PR branch, reintroducing the concurrent-ref race "+ "fixed in PR #5090."+ )++ def test_discover_has_invalid_slug_guard(self, workflow):+ run = _discover_run(workflow)+ assert "Invalid branch slug" in run, (+ "The invalid-slug guard is gone from discover's list step — a "+ "directory such as tests/a..b would enter the update matrix and "+ "fail only when create-pull-request tries to create its branch."+ )++ def test_discover_keeps_paths_nul_delimited_until_json_encoding(+ self, workflow+ ):+ run = _discover_run(workflow)+ assert "-printf '%h\\0'" in run, (+ "discover no longer emits NUL-delimited paths — a newline in a "+ "directory name could split one real path into fake matrix legs."+ )+ assert "read -r -d ''" in run, (+ "discover no longer reads the NUL-delimited path stream safely."+ )+def test_discover_permissions_are_read_only(self, workflow):perms = workflow["jobs"]["discover"].get("permissions", {})assert perms == {"contents": "read"}, (@@ -216,19 +236,24 @@ class TestWorkflowStructure:# With a branch name shared across legs, concurrent legs race on the# same ref ("Reference cannot be updated") and sequential legs# force-overwrite each other's commits, silently dropping updates —- # both observed in run #36. Pin both full expressions: the slug makes- # the branch unique per directory, run_number unique per run.+ # both observed in run #36. Pin a REGEX match, not a bare substring:+ # a mutation test showed a brace typo like+ # ``${ matrix.npm_directory.slug }}`` still contains many of the+ # same substrings (e.g. "matrix.npm_directory.slug") but is not a+ # well-formed GitHub Actions expression and would not expand,+ # silently breaking branch uniqueness while a substring check stays+ # green.+ slug_expr = re.compile(r"\$\{\{\s*matrix\.npm_directory\.slug\s*\}\}")for step in workflow["jobs"]["update"]["steps"]:if str(step.get("uses", "")).startswith("peter-evans/create-pull-request@"):branch = step["with"]["branch"]- assert (- "${{ steps.verify-changed-files.outputs.slug }}" in branch- ), (- "create-pull-request branch no longer expands the "- f"per-directory slug; got {branch!r}. All matrix legs "- "would write the same branch and clobber each other."+ assert slug_expr.search(branch), (+ "create-pull-request branch does not contain a "+ "well-formed ${{ matrix.npm_directory.slug }} "+ f"expression; got {branch!r}. All matrix legs would "+ "write the same branch and clobber each other.")assert "${{ github.run_number }}" in branch, ("create-pull-request branch no longer expands "@@ -276,6 +301,91 @@ def _matrix_from_output(tmp_path: Path) -> list[dict]:)+def _assert_branch_safe_slug(slug: str, context) -> None:+ """Assert ``slug`` yields a valid git ref when appended to the branch.++ Pure Python on purpose — it lets the test run without invoking+ ``git`` for every candidate slug. This encodes the subset of its+ rules a slug can actually violate: the slug is a *suffix* of+ ``update-npm-dependencies-{run_number}-``, so rules about a ref+ component's first character (a leading ``-`` or ``.``) cannot apply,+ but ``..``, a trailing ``.``, and a ``.lock`` suffix all can — and all+ three pass a bare ``[A-Za-z0-9._-]+`` charset check. A dir named+ ``tests/foo.lock`` or ``tests/a..b`` would slug cleanly and then fail+ in create-pull-request. Git's syntax checker also permits a branch that+ is too long for the runner filesystem to create with its temporary+ ``.lock`` suffix, so the policy caps slugs at 200 ASCII bytes.++ Deliberately *stricter* than git in three ways. An empty slug and a+ slug containing ``/`` both yield refs git would accept, but both mean+ the transform is broken (an empty slug collapses every leg onto one+ branch; a surviving ``/`` is the one character the transform exists to+ remove). Beyond that the charset itself is conservative: git accepts+ plenty of bytes it excludes (``+``, ``,``, ``@``, ``|``, non-ASCII+ such as ``café``), so a lockfile dir using them would trip this+ assertion despite producing a valid branch. That is the intended+ trade-off — such a dir should be renamed rather than have the guard+ loosened.++ The production discover step enforces the same policy in jq. The+ behavioral invalid-directory fixtures below execute that real script so+ this Python mirror cannot be the only effective guard.+ """+ # fullmatch, not match: "$" tolerates a trailing newline.+ assert re.fullmatch(r"[A-Za-z0-9._-]+", slug), (context, slug)+ assert ".." not in slug, (context, slug)+ assert not slug.endswith("."), (context, slug)+ assert not slug.endswith(".lock"), (context, slug)+ assert len(slug) <= _MAX_BRANCH_SLUG_LENGTH, (context, slug)+++class TestBranchSafeSlugHelper:+ """Unit-test the ref-validity rules themselves.++ Without this, the helper is decorative: no live repo path or fixture+ dir exercises the ``..`` / trailing-``.`` / ``.lock`` rules, so each+ could be deleted with the rest of the suite still green. These cases+ were cross-checked against the real ``git check-ref-format`` binary as+ ``refs/heads/update-npm-dependencies-42-<slug>`` — see the helper's+ docstring for the three places the rules are stricter than git on+ purpose.+ """++ @pytest.mark.parametrize(+ "slug",+ [+ "root",+ "tests",+ "tests-ui_tests-playwright",+ "tests-api_tests_with_login",+ "x.lockfile", # only an exact ".lock" suffix is invalid+ "x.LOCK", # git's .lock rule is case-sensitive+ "a.b", # a single dot is fine; only ".." is not+ "a" * _MAX_BRANCH_SLUG_LENGTH, # exact policy boundary+ ],+ )+ def test_accepts_valid_slugs(self, slug):+ _assert_branch_safe_slug(slug, "unit")++ @pytest.mark.parametrize(+ "slug",+ [+ "", # empty: every leg collapses onto one branch+ "a/b", # "/" is what the transform exists to remove+ "a..b", # git: ".." is never allowed in a ref+ "a.", # git: a ref component may not end in "."+ "a.lock", # git: a ref component may not end in ".lock"+ "a b", # git: no spaces+ "a~b", # git: no "~"+ "a:b", # git: no ":"+ "a" * (_MAX_BRANCH_SLUG_LENGTH + 1),+ ],+ )+ def test_rejects_invalid_slugs(self, slug):+ with pytest.raises(AssertionError):+ _assert_branch_safe_slug(slug, "unit")++@_REQUIRES_GNUclass TestPipelineBehavior:"""Execute the real find|jq pipeline against fixture trees."""@@ -291,9 +401,10 @@ class TestPipelineBehavior:"node_modules/evil/package-lock.json", # excluded".claude/evil/package-lock.json", # excluded (dot-dir)".github/evil/package-lock.json", # excluded (dot-dir)- # excluded (dot-dir): site-packages can ship npm lockfiles- # — a repo-root .venv must not trip the unknown-dir guard- # on dev checkouts.+ # excluded (dot-dir): a .venv can contain npm lockfiles+ # generated at runtime by packages' own node bootstraps+ # (e.g. readabilipy) — a repo-root .venv must not trip the+ # unknown-dir guard on dev checkouts.".venv/lib/pkg/js/package-lock.json",],)@@ -312,6 +423,10 @@ class TestPipelineBehavior:for d in ("tests/alpha", "tests/beta"):assert by_path[d]["build_cmd"] == "", dassert by_path[d]["test_cmd"].startswith("echo"), d+ # Slugs: "." -> "root", "/" -> "-" for everything else.+ assert by_path["."]["slug"] == "root"+ assert by_path["tests/alpha"]["slug"] == "tests-alpha"+ assert by_path["tests/beta"]["slug"] == "tests-beta"# Excluded dirs (node_modules anywhere, dot-dirs at root) never# appear in the matrix. The repo root "." is not a dot-dir.assert not any(@@ -320,7 +435,13 @@ class TestPipelineBehavior:), paths# Every entry carries the keys the update job dereferences.for e in matrix:- assert set(e) == {"path", "build_cmd", "test_cmd", "cache_path"}+ assert set(e) == {+ "path",+ "slug",+ "build_cmd",+ "test_cmd",+ "cache_path",+ }assert e["cache_path"] == "package-lock.json"def test_unknown_non_tests_path_fails_loudly(self, workflow, tmp_path):@@ -347,6 +468,81 @@ class TestPipelineBehavior:f"stderr={proc.stderr!r}")+ def test_colliding_slugs_fail_loudly(self, workflow, tmp_path):+ # tests/a-b and tests/a/b both gsub("/", "-") to "tests-a-b" — two+ # dirs would share one PR branch, reintroducing the concurrent-ref+ # race fixed in PR #5090. Both are under tests/, so they pass the+ # unknown-path guard cleanly; only the collision guard should fire.+ # tests/a.b is also included: an interleaving path is what separated+ # the duplicates under a locale-collating sort.+ root = tmp_path / "repo"+ _build_tree(+ root,+ [+ "package-lock.json",+ "tests/a-b/package-lock.json",+ "tests/a.b/package-lock.json",+ "tests/a/b/package-lock.json",+ ],+ )+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode != 0, (+ "colliding branch slugs must fail discovery, not silently enter "+ "the matrix"+ )+ # Pin the SPECIFIC failure mode so an unrelated shell error can't+ # satisfy this test accidentally.+ assert "Colliding branch slugs" in proc.stdout, (+ f"expected the slug-collision guard; got stdout={proc.stdout!r} "+ f"stderr={proc.stderr!r}"+ )++ @pytest.mark.parametrize(+ "invalid_dir",+ [+ "tests/a..b", # git: ".." is never allowed in a ref+ "tests/foo.", # git: a ref may not end in "."+ "tests/foo.lock", # git: a ref may not end in ".lock"+ "tests/a b", # git: no spaces+ "tests/a+b", # valid to git; excluded by the ASCII policy+ "tests/café", # valid to git; excluded by the ASCII policy+ pytest.param(+ "tests/a\ntests/b",+ id="embedded-newline",+ ),+ pytest.param(+ "tests/" + "a" * (_MAX_BRANCH_SLUG_LENGTH - len("tests-") + 1),+ id="slug-too-long",+ ),+ ],+ )+ def test_invalid_branch_slugs_fail_loudly(+ self, workflow, tmp_path, invalid_dir+ ):+ root = tmp_path / "repo"+ _build_tree(+ root,+ [+ "package-lock.json",+ f"{invalid_dir}/package-lock.json",+ ],+ )+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode != 0, (+ "an invalid branch slug must fail discovery, not reach "+ f"create-pull-request: dir={invalid_dir!r}"+ )+ assert "Invalid branch slug" in proc.stdout, (+ f"expected the invalid-slug guard; got stdout={proc.stdout!r} "+ f"stderr={proc.stderr!r}"+ )+ expected_path = json.dumps(invalid_dir, ensure_ascii=False)+ expected_slug = json.dumps(+ invalid_dir.replace("/", "-"), ensure_ascii=False+ )+ assert f"path={expected_path}" in proc.stdout, proc.stdout+ assert f"slug={expected_slug}" in proc.stdout, proc.stdout+def test_empty_matrix_guard_fails_loudly(self, workflow, tmp_path):# Only excluded dirs present => find yields nothing after filters,# so the guard must fail the step rather than silently no-op.@@ -442,44 +638,91 @@ class TestPipelineAgainstRealRepo:f"{[e['path'] for e in real_repo_matrix]}")+ def test_slug_matches_expected_formula_and_is_unique(+ self, real_repo_matrix+ ):+ # Pure-Python check of the documented "." -> "root", "/" -> "-"+ # formula against the REAL matrix output (not a re-implementation of+ # the discover pipeline itself) — confirms the live repo's slugs+ # match convention, fall in a branch-name-safe charset, and that no+ # two live dirs collide onto one branch.+ #+ # The '/'->'-' mapping is not injective (tests/foo-bar and+ # tests/foo/bar both slug to tests-foo-bar); no such pair exists+ # today, and this invariant keeps it that way as lockfile dirs are+ # added — the discover job itself also fails loudly on this (see+ # TestWorkflowStructure.test_discover_has_slug_collision_guard and+ # TestPipelineBehavior.test_colliding_slugs_fail_loudly).+ slugs = []+ for e in real_repo_matrix:+ expected = (+ "root" if e["path"] == "." else e["path"].replace("/", "-")+ )+ assert e["slug"] == expected, e+ _assert_branch_safe_slug(e["slug"], e["path"])+ slugs.append(e["slug"])+ assert len(set(slugs)) == len(slugs), (+ f"two live dirs slug to the same branch: {real_repo_matrix}"+ )+@_REQUIRES_GNUclass TestSlugBehavior:- """Execute the real branch-slug transform from the update job.-- The per-directory branch name is the whole race fix (run #36): these- run the actual ``echo "slug=..."`` line extracted from the YAML, so a- broken sed, an emptied slug, or a slug collision cannot pass unnoticed.+ """Derive branch slugs from the REAL executed discover pipeline.++ The per-directory branch slug used to be produced by a separate sed+ transform simulated in these tests; it is now computed once, in+ discover's jq, as ``matrix.npm_directory.slug``. This test reads+ slugs off the matrix output produced by actually running the find|jq+ pipeline (see ``_run_pipeline``), so it cannot drift from the+ workflow.++ Scope, deliberately: this class asserts only *properties* every valid+ slug must hold — ``.`` maps to ``root``, the charset is branch-safe,+ and no two dirs collide. Those properties hold for any injective+ transform, so this class alone would NOT catch the transform changing+ (e.g. ``/`` -> ``_``). The intended transform is pinned separately, by+ the hardcoded expectations in+ ``TestPipelineBehavior.test_classifies_root_and_test_dirs`` and the+ formula check in ``TestPipelineAgainstRealRepo``'s+ ``test_slug_matches_expected_formula_and_is_unique``; a collision is+ caught behaviorally by+ ``TestPipelineBehavior.test_colliding_slugs_fail_loudly``."""- def test_slug_maps_fixture_dirs_to_unique_valid_refs(self, workflow):- slug_line = _slug_line(workflow)- dirs = [- ".",- "tests",- "tests/ui_tests",- "tests/ui_tests/playwright",- "tests/api_tests_with_login",- ]- slugs = {d: _slug_for(slug_line, d) for d in dirs}- assert slugs["."] == "root", slugs- assert len(set(slugs.values())) == len(dirs), (- f"slug collision — two dirs would share a branch: {slugs}"- )- for d, slug in slugs.items():- assert slug and "/" not in slug, (d, slug)-- def test_real_repo_dirs_slug_to_unique_branches(- self, workflow, real_repo_matrix+ def test_slug_maps_fixture_dirs_to_unique_valid_refs(+ self, workflow, tmp_path):- # The '/'->'-' mapping is not injective (tests/foo-bar and- # tests/foo/bar both slug to tests-foo-bar, silently sharing a- # branch). No such pair exists today; this invariant keeps it that- # way as lockfile dirs are added.- slug_line = _slug_line(workflow)- slugs = {- e["path"]: _slug_for(slug_line, e["path"]) for e in real_repo_matrix- }+ root = tmp_path / "repo"+ max_length_dir = "tests/" + "a" * (+ _MAX_BRANCH_SLUG_LENGTH - len("tests-")+ )+ _build_tree(+ root,+ [+ "package-lock.json",+ "tests/package-lock.json",+ "tests/ui_tests/package-lock.json",+ "tests/ui_tests/playwright/package-lock.json",+ "tests/api_tests_with_login/package-lock.json",+ "tests/x.lockfile/package-lock.json",+ "tests/x.LOCK/package-lock.json",+ "tests/a.b/package-lock.json",+ f"{max_length_dir}/package-lock.json",+ ],+ )+ proc = _run_pipeline(_discover_run(workflow), root, tmp_path)+ assert proc.returncode == 0, proc.stderr+ matrix = _matrix_from_output(tmp_path)+ slugs = {e["path"]: e["slug"] for e in matrix}+ assert slugs["."] == "root", slugs+ assert len(slugs[max_length_dir]) == _MAX_BRANCH_SLUG_LENGTHassert len(set(slugs.values())) == len(slugs), (- f"two live dirs slug to the same branch: {slugs}"+ f"slug collision — two dirs would share a branch: {slugs}")+ for path, slug in slugs.items():+ # "/" must be gone — it is the one character the transform+ # exists to remove, and the charset check below would catch it+ # anyway; asserted explicitly so a regression names the cause.+ assert "/" not in slug, (path, slug)+ _assert_branch_safe_slug(slug, path)