diff --git a/.github/assign-reviewers/README.md b/.github/assign-reviewers/README.md index 87b63842..7967b4d2 100644 --- a/.github/assign-reviewers/README.md +++ b/.github/assign-reviewers/README.md @@ -51,6 +51,45 @@ Add parser cases **to the corpus**, not as an inline literal in either suite — a case only one implementation ever sees proves nothing about the other, which is how the two live divergences reached `main`. +Four dialect rules the corpus now pins on both sides, because each was a place +the ports read the same bytes differently: a **duplicate top-level +`default_pool:` is last-wins** (the second replaces the first — the block arm +used to *append* on the JS side — and each port warns rather than rejecting; +a repeated `rules:` is *not* covered, it still appends on both sides); a **`#` +opens a comment only at column 0 or after a space or a tab**, the two characters +spelled out rather than delegated to `/\s/` and `isspace()`, which disagree about +U+0085, U+001C and U+FEFF; a **single leading U+FEFF is stripped from the +document**, which `trim()` did for free and `strip()` did not; and **a token is +trimmed of a space or a tab and nothing else** — YAML s-white, spelled out as +`_trim` in the Python port and `trimSWhite` in the JS one, because `str.strip()` +also drops U+0085 and U+001C-U+001F, `String.prototype.trim()` also drops U+FEFF, +and *both* drop U+00A0. Every other invisible character now survives into the +login verbatim on both sides, so a padded login fails to route identically in the +runtime and in the drift generator instead of routing in one and not the other. +The warning text is the one part not corpus-pinned — the channels differ +(`core.warning` vs a `::warning::` line) — so each suite asserts its own. + +Three consequences of that narrowing, each pinned rather than left to be rediscovered: +the runtime **warns** when a *configured* login fails the login-shape gate, because the +padding that now survives is invisible and the token would otherwise just never be +assigned (keyed on the shape test only — being excluded as the PR author is normal and +must not warn), sweeping **every** rule's reviewers and the whole `default_pool` up +front rather than only the ones a given run routes through, since the rot belongs to the +file and not to the diff — the sweep warns, it does not enqueue, so an unmatched rule's +owners are still never candidates; a line whose only content is non-s-white whitespace is **no longer +blank**, and since indentation counts spaces it reads as column 0 and terminates the +block above it, which inside `rules:` drops every later rule; and the `setKey` regex +spells its class out as `[^\n]*` rather than `.`, because Python's `.` excludes only LF +while JS's also excludes CR, U+2028 and U+2029 — with `.` the Python port matched a rule +line ending in a bare CR and the JS port did not, so the runtime dropped the key while +the generator modelled those reviewers as routing. + +The generator reads the committed config as **bytes**, never with `text=True`: universal- +newline translation rewrites CRLF and a bare CR to LF before the parser sees them, which +would hand the parser different bytes than the runtime reads from the base64 blob — the +same divergence class, one layer above the parser and invisible to a test that feeds the +parser text directly. + The [caller guide](../../docs/callers/assign-reviewers.md) documents the ranking, evidence limits, failure handling, and configuration. Keep behavior there rather than maintaining another algorithm description here. diff --git a/.github/assign-reviewers/parser-corpus.json b/.github/assign-reviewers/parser-corpus.json index c6b689d5..abe83728 100644 --- a/.github/assign-reviewers/parser-corpus.json +++ b/.github/assign-reviewers/parser-corpus.json @@ -14,6 +14,8 @@ "(config, locations) and only the config half is compared here, because `locations` has no JS", "counterpart and stays covered by test_generate.py's own rewrite tests.", "`globs[].cases[]` are path/verdict pairs for the glob->RegExp translator.", + "Characters that would be invisible or ambiguous in this file (U+FEFF, U+0085, U+001C, U+00A0)", + "are written as JSON \\u escapes on purpose — keep them escaped rather than pasting the literal.", "", "Both path filters (test-assign-reviewers.yml, test-refresh-reviewers.yml) watch this file, so", "editing it runs both suites — a corpus tweak that only satisfies one port cannot land green." @@ -138,6 +140,252 @@ {"paths": ["a/**"], "reviewers": ["bob"]} ] } + }, + { + "name": "duplicate default_pool, flow then block", + "why": "YAML requires unique keys and both ports take PyYAML's de-facto last-wins, so the block form must REPLACE the flow list instead of appending to it", + "text": "default_pool: [alice]\ndefault_pool:\n - bob\n", + "expected": { + "default_pool": ["bob"], + "rules": [] + } + }, + { + "name": "duplicate default_pool, block then flow", + "why": "last-wins in the other order; the flow arm always replaced, and this pins that it keeps doing so", + "text": "default_pool:\n - alice\ndefault_pool: [bob]\n", + "expected": { + "default_pool": ["bob"], + "rules": [] + } + }, + { + "name": "duplicate default_pool, second one empty", + "why": "last-wins means an empty second key EMPTIES the pool, and the `rules:` after it still parses", + "text": "default_pool: [alice]\ndefault_pool:\nrules:\n - paths: [a]\n reviewers: [b]\n", + "expected": { + "default_pool": [], + "rules": [ + {"paths": ["a"], "reviewers": ["b"]} + ] + } + }, + { + "name": "leading BOM before the first key", + "why": "a leading U+FEFF is legal YAML; JS trim() tolerated it and Python strip() did not, so the BOM is now stripped from the document on both sides", + "text": "\ufeffdefault_pool: [alice]\nrules:\n - paths: [\"src/**\"]\n reviewers: [bob]\n", + "expected": { + "default_pool": ["alice"], + "rules": [ + {"paths": ["src/**"], "reviewers": ["bob"]} + ] + } + }, + { + "name": "leading BOM before a comment line", + "why": "the BOM strip and the space-or-tab comment rule must agree on the commonest BOM document of all, one that opens with a header comment", + "text": "\ufeff# hdr\ndefault_pool: [alice]\n", + "expected": { + "default_pool": ["alice"], + "rules": [] + } + }, + { + "name": "U+0085 before # is not a comment", + "why": "NEL is isspace() in Python but not /\\s/ in JS, so the ports disagreed until both required a space or a tab", + "text": "default_pool: [x\u0085#c, y]\n", + "expected": { + "default_pool": ["x\u0085#c", "y"], + "rules": [] + } + }, + { + "name": "U+FEFF before # is not a comment", + "why": "the mirror image: U+FEFF is /\\s/ in JS but not isspace() in Python; only a MID-document BOM reaches this test, the leading one is stripped first", + "text": "default_pool: [x\ufeff#c, y]\n", + "expected": { + "default_pool": ["x\ufeff#c", "y"], + "rules": [] + } + }, + { + "name": "U+001C before # is not a comment", + "why": "FILE SEPARATOR is isspace() in Python but not /\\s/ in JS, the same disagreement as U+0085 from a different character class", + "text": "default_pool: [x\u001c#c, y]\n", + "expected": { + "default_pool": ["x\u001c#c", "y"], + "rules": [] + } + }, + { + "name": "tab before # starts a comment", + "why": "s-white is space OR tab, so narrowing the rule must not drop the tab half", + "text": "default_pool: [x\t#c, y]\n", + "expected": { + "default_pool": ["x"], + "rules": [] + } + }, + { + "name": "space before # starts a comment", + "why": "the other half of s-white, pinned beside the tab so neither can be narrowed away", + "text": "default_pool: [x #c, y]\n", + "expected": { + "default_pool": ["x"], + "rules": [] + } + }, + { + "name": "U+FEFF at the start of a flow token is kept", + "why": "JS trim() strips U+FEFF and Python strip() does not, so this token used to arrive with the BOM on one port and without it on the other; s-white trims neither", + "text": "default_pool: [\ufeffalice]\n", + "expected": { + "default_pool": ["\ufeffalice"], + "rules": [] + } + }, + { + "name": "U+0085 at the end of a flow token is kept", + "why": "the mirror image: Python strip() strips NEL and JS trim() does not; s-white trims neither", + "text": "default_pool: [x\u0085, y]\n", + "expected": { + "default_pool": ["x\u0085", "y"], + "rules": [] + } + }, + { + "name": "U+001C at the end of a flow token is kept", + "why": "Python strip() strips FILE SEPARATOR and JS trim() does not, the same disagreement as U+0085 from a different character class", + "text": "default_pool: [x\u001c, y]\n", + "expected": { + "default_pool": ["x\u001c", "y"], + "rules": [] + } + }, + { + "name": "U+00A0 padding around a flow token is kept", + "why": "the one character both ports agreed on and both got wrong — strip() and trim() BOTH drop NBSP, so a login padded with it used to look routable; s-white keeps it literal, so the runtime and the drift generator now agree it does not route", + "text": "default_pool: [\u00a0x\u00a0, y]\n", + "expected": { + "default_pool": ["\u00a0x\u00a0", "y"], + "rules": [] + } + }, + { + "name": "U+FEFF at the start of a block item is kept", + "why": "the block arm trims separately from the flow arm, so the JS-strips-BOM divergence has to be pinned on both; only a MID-document BOM reaches here, the leading one is stripped first", + "text": "default_pool:\n - \ufeffalice\n", + "expected": { + "default_pool": ["\ufeffalice"], + "rules": [] + } + }, + { + "name": "U+0085 at the end of a block item is kept", + "why": "Python strip() used to drop the trailing NEL from a block item while JS trim() kept it", + "text": "default_pool:\n - alice\u0085\n", + "expected": { + "default_pool": ["alice\u0085"], + "rules": [] + } + }, + { + "name": "U+FEFF inside a rule's flow reviewers is kept", + "why": "a rule's reviewers list trims through its own path (setKey / set_key, then unquote), so the JS-strips-BOM divergence is pinned there too", + "text": "rules:\n - paths: [src/**]\n reviewers: [\ufeffbob]\n", + "expected": { + "default_pool": [], + "rules": [ + {"paths": ["src/**"], "reviewers": ["\ufeffbob"]} + ] + } + }, + { + "name": "U+001C at the end of a block reviewer is kept", + "why": "the last trim path left: a block reviewer under a rule, where Python strip() used to drop the trailing FILE SEPARATOR and JS trim() kept it", + "text": "rules:\n - paths:\n - src/**\n reviewers:\n - bob\u001c\n", + "expected": { + "default_pool": [], + "rules": [ + {"paths": ["src/**"], "reviewers": ["bob\u001c"]} + ] + } + }, + { + "name": "two leading BOMs: only one is stripped", + "why": "the explicit single-BOM strip is now the ONLY thing removing a BOM on either port; JS trim() used to absorb the second one and parse the key, Python strip() never did", + "text": "\ufeff\ufeffdefault_pool: [alice]\n", + "expected": { + "default_pool": [], + "rules": [] + } + }, + { + "name": "U+0085 before a top-level key is not indentation", + "why": "Python strip() used to remove the leading NEL so the line still matched `default_pool:`, while JS trim() left it and skipped the line; s-white leaves it on both, and indentOf/_indent_of count spaces only, so the line is column 0 either way", + "text": "\u0085default_pool: [alice]\n", + "expected": { + "default_pool": [], + "rules": [] + } + }, + { + "name": "tab padding around a flow token is trimmed", + "why": "narrowing the trim to s-white must not drop the tab half of it — this is the case that reds if either port is retuned to trim spaces only", + "text": "default_pool: [\tx\t, y]\n", + "expected": { + "default_pool": ["x", "y"], + "rules": [] + } + }, + { + "name": "a bare CR ending the last line is data, not a line break", + "why": "a CR is a line break only when a newline FOLLOWS it — JS splits on /\\r?\\n/ and keeps this one, and the Python port must split the same way rather than stripping a trailing CR off each piece of a split(\"\\n\"), which would eat it. The runtime is the reference: the drift generator has to model what routes, and neither side trims CR as s-white any more", + "text": "default_pool:\n - alice\r", + "expected": { + "default_pool": ["alice\r"], + "rules": [] + } + }, + { + "name": "a bare CR ending a rule key line is data, and BOTH ports keep the key", + "why": "the two languages' `.` differ: Python's omits only LF, JS's also omits CR, U+2028 and U+2029. With `(.*)$` the Python setKey regex matched this line and the JS one did NOT, so the runtime dropped `reviewers:` entirely and routed nobody while the drift generator modelled alice as routing. Both ports now spell the class out as `[^\\n]*`, keep the CR as part of the value, and agree it cannot be a login", + "text": "rules:\n - paths: ['a/**']\n reviewers: alice\r", + "expected": { + "default_pool": [], + "rules": [ + {"paths": ["a/**"], "reviewers": ["alice\r"]} + ] + } + }, + { + "name": "a U+00A0-only line at column 0 ends a default_pool block on both ports", + "why": "narrowing the blank-line test to s-white means a line whose only content is non-s-white whitespace is no longer blank, and `_indent_of`/`indentOf` count SPACES only, so it reads as column 0 and terminates the block. Both ports moved together, so this is a behaviour change rather than a parity break — pinned because nothing else covers a whitespace-only line and the truncation is otherwise silent", + "text": "default_pool:\n - alice\n\u00a0\n - bob\n", + "expected": { + "default_pool": ["alice"], + "rules": [] + } + }, + { + "name": "a U+00A0-only line at column 0 drops every later rule on both ports", + "why": "same mechanism as the default_pool case, but inside the `rules:` loop the `break` discards every rule AFTER the stray line, not just the tail of one list. Pinned so the blast radius of one invisible character is executable on both ports rather than discovered in a consumer repo", + "text": "rules:\n - paths: ['a/**']\n reviewers: [alice]\n\u00a0\n - paths: ['b/**']\n reviewers: [bob]\n", + "expected": { + "default_pool": [], + "rules": [ + {"paths": ["a/**"], "reviewers": ["alice"]} + ] + } + }, + { + "name": "an INDENTED U+00A0-only line does not end the block", + "why": "the negative control for the two cases above: `_indent_of`/`indentOf` count the leading spaces, so a padded blank line that is INDENTED reads as column 2, never reaches the column-0 `break`, and is simply not a `- ` item. Bounds the truncation to a stray character at column 0 specifically", + "text": "default_pool:\n - alice\n \u00a0\n - bob\n", + "expected": { + "default_pool": ["alice", "bob"], + "rules": [] + } } ], "globs": [ diff --git a/.github/assign-reviewers/tests/assignment.test.cjs b/.github/assign-reviewers/tests/assignment.test.cjs index 801cb387..4776b92d 100644 --- a/.github/assign-reviewers/tests/assignment.test.cjs +++ b/.github/assign-reviewers/tests/assignment.test.cjs @@ -63,7 +63,15 @@ const runScript = new AsyncFunction('github', 'context', 'core', 'process', 'req // design, so they take the sentinel and indent checks but not the whole-scalar bracket check; // their origin offset keeps reported line numbers absolute in assign-reviewers.yml. const SCRIPT_ORIGIN = {file: 'assign-reviewers.yml', offset: scriptSpan.begin + 1}; -const helpers = new Function(`${region(script, 'glob-matcher', SCRIPT_ORIGIN)}\n${region(script, 'config-parser', SCRIPT_ORIGIN)}\nreturn {globToRegExp, matchesAny, parseReviewerConfig};`)(); +// `core` is ambient inside a github-script step but NOT inside this `new Function` scope, so the +// extracted region has to be handed one. It is a real capture, not a silencer: `parseReviewerConfig` +// calls `core.warning` on a duplicate top-level `default_pool:`, and `helperWarnings` is what lets a +// test assert that warning fires. Leave it out and the region throws ReferenceError on that path — +// which is a REAL failure mode of the shipped script too, so extend the stub rather than the sentinels +// whenever the parser reaches for another `core` method. +const helperWarnings = []; +const helperCore = {warning: (message) => helperWarnings.push(message), info: () => {}}; +const helpers = new Function('core', `${region(script, 'glob-matcher', SCRIPT_ORIGIN)}\n${region(script, 'config-parser', SCRIPT_ORIGIN)}\nreturn {globToRegExp, matchesAny, parseReviewerConfig};`)(helperCore); const corpus = JSON.parse(readFileSync(resolve(__dirname, '../parser-corpus.json'), 'utf8')); const file = (filename) => ({filename, changes: 10}); const approval = (login, state = 'APPROVED', type = 'User') => ({user: {login, type}, state, author_association: 'MEMBER'}); @@ -381,6 +389,98 @@ for (const {name, text, expected} of corpus.configs) { assert.deepEqual(helpers.parseReviewerConfig(text), expected); }); } +// The corpus compares parsed CONFIGS, which is deliberately silent about the duplicate-key warning +// — the Python port prints its own `::warning::` line instead, so the text cannot live in the shared +// fixture. Each side therefore asserts its own channel; last-wins itself stays corpus-pinned above. +test('a duplicate top-level default_pool: warns once, naming the key', () => { + helperWarnings.length = 0; + assert.deepEqual(helpers.parseReviewerConfig('default_pool: [alice]\ndefault_pool:\n - bob\n').default_pool, ['bob']); + assert.equal(helperWarnings.length, 1); + assert.match(helperWarnings[0], /duplicate top-level `default_pool:` key/); +}); + +test('the duplicate warning names the configured path, and omits the prefix without one', () => { + // `reviewer_config_path` is a caller input, so the warning must not hardcode + // `reviewers.yml` — a caller that configured another name would be told to go + // look at a file its repo does not have. + helperWarnings.length = 0; + helpers.parseReviewerConfig('default_pool: [alice]\ndefault_pool: [bob]\n', '.github/owners.yml'); + assert.equal(helperWarnings.length, 1); + assert.match(helperWarnings[0], /^\.github\/owners\.yml: duplicate top-level/); + // Omitted (as the harness calls it): a bare message, never a literal `undefined:`. + helperWarnings.length = 0; + helpers.parseReviewerConfig('default_pool: [alice]\ndefault_pool: [bob]\n'); + assert.equal(helperWarnings.length, 1); + assert.doesNotMatch(helperWarnings[0], /undefined/); + assert.match(helperWarnings[0], /^duplicate top-level/); +}); + +test('an empty first default_pool: still warns on the duplicate', () => { + // Keyed on "the key was seen", not on "the list is non-empty" — an emptiness test would make the + // JS warn where the Python port (which keys on its own seen-flag) does not. + helperWarnings.length = 0; + assert.deepEqual(helpers.parseReviewerConfig('default_pool: []\ndefault_pool: [bob]\n').default_pool, ['bob']); + assert.equal(helperWarnings.length, 1); +}); +test('a single default_pool: is silent, however it is written', () => { + helperWarnings.length = 0; + for (const text of ['default_pool: [alice]\n', 'default_pool:\n - alice\n', 'rules:\n - reviewers: [a]\n', ' default_pool: [indented]\ndefault_pool: [alice]\n']) { + helpers.parseReviewerConfig(text); + } + assert.deepEqual(helperWarnings, []); +}); + +// The invalid-configured-login warning. Unlike the duplicate-key warning above this one lives in +// the routing script rather than the shared parser region, so it is driven through `run()` and read +// off `logs` — and it only exists because both ports narrowed to trimming s-white, which left a +// login padded with U+00A0/U+FEFF/U+0085 surviving the parse verbatim and then failing the shape +// gate, invisibly. `\u00a0` is written escaped on purpose: a literal here would go vacuous the +// moment an editor normalised it away, with every assertion below still passing. +const NBSP = '\u00a0'; +test('a padded login in a MATCHED rule warns, codepoint-escaped, and does not route', async () => { + const result = await run({config: `rules:\n - paths: ["src/**"]\n reviewers: ["${NBSP}alice", bob]\n`}); + assert.deepEqual(result.selected, ['bob']); + const warnings = result.logs.filter((line) => /is not a valid GitHub login/.test(line)); + assert.equal(warnings.length, 1); + assert.match(warnings[0], /configured reviewer "\\u00a0alice"/); + assert.match(warnings[0], /^\.github\/reviewers\.yml: /); +}); +test('a padded login warns even when its rule matches NOTHING in this PR', async () => { + // The regression CodeRabbit caught: warning only from `configuredCandidate` reached a rule's + // reviewers only once that rule's paths matched a changed file, so rot in any other area of the + // config stayed silent on every run that did not happen to touch it. The rot is a property of + // the FILE, not of this diff. + const result = await run({ + files: [file('src/api/main.js')], + config: `rules:\n - paths: ["src/api/**"]\n reviewers: [alice]\n - paths: ["docs/**"]\n reviewers: ["${NBSP}carol"]\n`, + }); + assert.deepEqual(result.selected, ['alice']); + // Warned about, but emphatically NOT routed: an unmatched rule's owners stay out of `candidates`. + assert.equal(result.logs.filter((line) => /configured reviewer "\\u00a0carol"/.test(line)).length, 1); + assert.ok(!result.selected.includes('carol')); +}); +test('a padded default_pool entry warns on a run that never reaches the fallback', async () => { + // `default_pool` was only ever swept when NO rule and no history covered any file. A rotted + // entry in it could therefore go unreported indefinitely while some rule kept matching — and + // the fallback is exactly the path you need it to work on when it finally fires. + const result = await run({config: `default_pool: ["bob${NBSP}"]\nrules:\n - paths: ["src/**"]\n reviewers: [alice]\n`}); + assert.deepEqual(result.selected, ['alice']); + assert.equal(result.logs.filter((line) => /configured reviewer "bob\\u00a0"/.test(line)).length, 1); +}); +test('each bad token warns once, however many rules repeat it', async () => { + const result = await run({ + config: `default_pool: ["${NBSP}dana"]\nrules:\n - paths: ["src/api/**"]\n reviewers: ["${NBSP}dana", alice]\n - paths: ["docs/**"]\n reviewers: ["${NBSP}dana"]\n`, + }); + assert.equal(result.logs.filter((line) => /configured reviewer "\\u00a0dana"/.test(line)).length, 1); +}); +test('a well-formed login is never warned about, excluded or not', async () => { + // Keyed on the SHAPE test only, never on `eligible()`: the exclude set legitimately holds the PR + // author and `EXCLUDE`, so warning there would fire on essentially every run and train people to + // ignore the message. `author` and `alice` below are both shape-valid and both excluded. + const result = await run({config: 'rules:\n - paths: ["src/**"]\n reviewers: [Author, Alice, bob]\n', env: {EXCLUDE: '@ALICE'}}); + assert.deepEqual(result.selected, ['bob']); + assert.deepEqual(result.logs.filter((line) => /is not a valid GitHub login/.test(line)), []); +}); for (const {glob, cases} of corpus.globs) { test(`corpus glob — ${glob}`, () => { for (const {path, matches} of cases) { diff --git a/.github/refresh-reviewers/generate.py b/.github/refresh-reviewers/generate.py index b302a60c..2ce2fefe 100644 --- a/.github/refresh-reviewers/generate.py +++ b/.github/refresh-reviewers/generate.py @@ -166,11 +166,25 @@ def _strip_comment(s): in_s = not in_s elif ch == '"' and not in_s: in_d = not in_d - elif ch == "#" and not in_s and not in_d and (i == 0 or s[i - 1].isspace()): + # YAML starts a comment at `#` only at column 0 or after s-white — a SPACE + # or a TAB, nothing else. `isspace()` was wider here and `/\s/` wider + # (differently) in the JS port, so the two disagreed on U+0085, U+001C and + # U+FEFF; both now spell out the two characters. Pinned by the corpus. + elif ch == "#" and not in_s and not in_d and (i == 0 or s[i - 1] in " \t"): return s[:i] return s +# YAML s-white: the ONLY characters trimmed around a token. Spelled out (not +# str.strip()) because strip() and JS trim() disagree about U+FEFF, U+0085 and +# U+001C-U+001F; the JS port's `trimSWhite` is the same two characters. Corpus-pinned. +S_WHITE = " \t" + + +def _trim(s): + return s.strip(S_WHITE) + + def _find_unquoted(s, ch, start=0): r"""Index of the first `ch` at or after `start` that sits OUTSIDE a quoted scalar, else -1. Quote state is always tracked from index 0, so `start` @@ -194,14 +208,14 @@ def _find_unquoted(s, ch, start=0): def _unquote(s): - s = s.strip() + s = _trim(s) if len(s) >= 2 and ((s[0] == '"' and s[-1] == '"') or (s[0] == "'" and s[-1] == "'")): return s[1:-1] return s def _parse_flow(s): - s = s.strip() + s = _trim(s) if not s.startswith("["): return None end = s.find("]") @@ -269,21 +283,49 @@ def parse_reviewer_config(text): locations = {"default_pool": loc-or-None, "rules": [loc-or-None, ...]} (reviewers-list positions) """ - raw_lines = text.split("\n") + # A single leading U+FEFF is legal YAML and must not become part of the first + # key; neither `strip()` nor `_trim` treats it as whitespace, so the first + # `default_pool:` of a BOM-prefixed document used to be skipped here while the + # JS port (whose `trim()` does strip it) parsed the same bytes fine. Dropping + # one character from the head of line 0 leaves every line INDEX untouched, so + # `locs` — and the byte-faithful `rewrite_config`, which keeps the BOM by + # splitting `text` itself — are unaffected. + # + # The CR of a CRLF document used to come off incidentally, via the `strip()` + # calls that `_trim` replaced; s-white excludes CR, so consume it HERE instead, + # as part of the line break. `re.split(r"\r?\n", ...)` is the JS port's + # `split(/\r?\n/)` character for character, which is the point: a CR is only + # a line break when a newline FOLLOWS it. Stripping a trailing CR off each + # `split("\n")` piece instead — the obvious shortcut — also eats a BARE CR + # ending the last line of a document with no final newline, which JS keeps as + # data; that would have traded the old divergence for a new one. Splitting on + # the two-character break produces the same number of lines as `split("\n")` + # on a CRLF document, so every line index is left alone. + raw_lines = re.split(r"\r?\n", text.removeprefix("\ufeff")) lines = [_strip_comment(l) for l in raw_lines] config = {"default_pool": [], "rules": []} locs = {"default_pool": None, "rules": [], "rule_paths": []} + seen_default_pool = False i = 0 n = len(lines) while i < n: raw = lines[i] - line = raw.strip() + line = _trim(raw) if not line: i += 1 continue if _indent_of(raw) == 0 and line.startswith("default_pool:"): - rest = line[len("default_pool:"):].strip() + # YAML requires unique keys; PyYAML's de-facto last-wins is the + # reference. Warn, never reject — the drift generator must not + # hard-fail on a malformed map. Both the flow and the block arm below + # already REPLACE `config["default_pool"]`, so last-wins needs no + # further work here; each arm also REPOINTS `locs["default_pool"]`, + # so the rewrite can never target an occurrence the parse discarded. + if seen_default_pool: + print("::warning::duplicate top-level default_pool: key — last one wins") + seen_default_pool = True + rest = _trim(line[len("default_pool:"):]) flow = _parse_flow(rest) if flow is not None: config["default_pool"] = flow @@ -299,20 +341,26 @@ def parse_reviewer_config(text): indent = 2 while i < n: r = lines[i] - if not r.strip(): + if not _trim(r): i += 1 continue if _indent_of(r) == 0: break - t = r.strip() + t = _trim(r) if t.startswith("- "): items.append(_unquote(t[2:])) item_lines.append(i) indent = _indent_of(r) i += 1 config["default_pool"] = items - if item_lines: - locs["default_pool"] = ("block", item_lines, indent) + # Last-wins has to move `locs` too, including when the winning + # occurrence carries NO item lines. Leaving it on a shadowed earlier + # occurrence would point `rewrite_config` at a line the very next + # parse throws away: the emitted file would carry the refreshed pool + # on the dead key, still re-read as the empty winner, and every + # scheduled run would regenerate the same no-op diff forever. `None` + # means "no rewritable list here", which `rewrite_config` skips. + locs["default_pool"] = ("block", item_lines, indent) if item_lines else None continue if _indent_of(raw) == 0 and line.startswith("rules:"): i += 1 @@ -323,10 +371,20 @@ def parse_reviewer_config(text): def set_key(seg, line_idx): nonlocal list_key - m = re.match(r"^(paths|reviewers):(.*)$", seg) + # `[^\n]*`, not `.*`: the two languages disagree about what `.` + # excludes. Python's `.` omits only LF, JS's omits LF, CR, U+2028 + # and U+2029 — so on a rule line ending in a bare CR (the last line + # of a document with no final newline, which the split above now + # preserves as data) Python matched and JS did NOT, silently + # dropping the key and leaving the rule with no reviewers while + # this generator modelled those reviewers as routing. Spelled out + # on both ports, both now keep the CR as part of the value — where + # `eligible()` rejects it, the same answer the corpus already pins + # for the `default_pool:` block arm. + m = re.match(r"^(paths|reviewers):([^\n]*)$", seg) if not m: return - key, val = m.group(1), m.group(2).strip() + key, val = m.group(1), _trim(m.group(2)) flow = _parse_flow(val) if flow is not None: if current is not None: @@ -357,13 +415,13 @@ def set_key(seg, line_idx): while i < n: r = lines[i] - if not r.strip(): + if not _trim(r): i += 1 continue if _indent_of(r) == 0: break ind = _indent_of(r) - t = r.strip() + t = _trim(r) is_dash = t == "-" or t.startswith("- ") if is_dash and (rule_indent == -1 or ind == rule_indent): if rule_indent == -1: @@ -373,11 +431,11 @@ def set_key(seg, line_idx): config["rules"].append(current) locs["rules"].append(cur_loc) list_key = None - after_dash = t[1:].strip() + after_dash = _trim(t[1:]) if after_dash: set_key(after_dash, i) elif is_dash and list_key and current is not None: - current[list_key].append(_unquote(t[1:].strip())) + current[list_key].append(_unquote(_trim(t[1:]))) if list_key == "reviewers": if cur_loc["reviewers"] is None: cur_loc["reviewers"] = ("block", [], ind) @@ -465,14 +523,35 @@ def _rewrite_flow_line(line, key, logins): new_list = "[" + ", ".join(logins) + "]" stripped = _strip_comment(line) key_pos = stripped.find(key) - open_idx = _find_unquoted(stripped, "[", key_pos) + # The flow span counts only when the VALUE ITSELF opens with `[`, exactly as + # `_parse_flow` requires before it returns a flow list. Searching for any `[` + # after the key also matches one sitting INSIDE a plain scalar, which the + # s-white comment rule makes reachable: in `reviewers: alice # see [bob]` + # the `#` no longer opens a comment, so the whole tail is the value — and the + # old search rewrote the bracket in that comment-shaped tail while leaving the + # real value `alice` in place. Anchoring here keeps the two in step. + value_pos = key_pos + len(key) + rest = stripped[value_pos:] + # s-white, not bare lstrip()/rstrip(): the rewriter has to agree with + # `_parse_flow` about where the VALUE starts and ends, and `_parse_flow` now + # trims s-white only. A bare `lstrip()` still absorbs U+00A0 and U+001C, so + # `reviewers:[alice]` — a bare SCALAR to the parser — took the bracket + # branch here and emitted `reviewers:[new-a, new-b]`, which re-parses as + # ONE ineligible scalar login: the refreshed rule routed nobody. The trailing + # `rstrip()` was the mirror image, leaving value bytes outside the span it + # replaced. Both ends now spell out the same two characters the parser does. + # The close-bracket scan is quote-aware too (`_find_unquoted`, not `find`): + # `reviewers: ["a]b", carol,` must not treat the `]` inside the quoted + # scalar as the end of the sequence. + lead = len(rest) - len(rest.lstrip(S_WHITE)) + open_idx = value_pos + lead if rest[lead:lead + 1] == "[" else -1 if open_idx != -1: close_idx = _find_unquoted(stripped, "]", open_idx) - end = close_idx + 1 if close_idx != -1 else len(stripped.rstrip()) + end = close_idx + 1 if close_idx != -1 else len(stripped.rstrip(S_WHITE)) return line[:open_idx] + new_list + line[end:] # scalar form: `reviewers: alice # note` -> replace the value span only key_end = key_pos + len(key) - return line[:key_end] + " " + new_list + line[len(stripped.rstrip()):] + return line[:key_end] + " " + new_list + line[len(stripped.rstrip(S_WHITE)):] def rewrite_config(text, locs, rule_replacements, default_pool_replacement): diff --git a/.github/refresh-reviewers/tests/test_generate.py b/.github/refresh-reviewers/tests/test_generate.py index 57693cd5..4183eb63 100644 --- a/.github/refresh-reviewers/tests/test_generate.py +++ b/.github/refresh-reviewers/tests/test_generate.py @@ -22,7 +22,9 @@ Run: python3 -m unittest discover -s .github/refresh-reviewers/tests -p 'test_*.py' -v """ +import contextlib import importlib.util +import io import json import os import re @@ -352,6 +354,74 @@ def test_flow_rewrite_preserves_comments(self): self.assertIn("reviewers: [old-f] # keep: cold-start", out) self.assertIn("# Reviewer expertise map — hand-tuned", out) + def test_flow_span_is_anchored_to_the_value_not_to_any_later_bracket(self): + # Fallout of narrowing `_strip_comment` to s-white: after a character + # that is NOT a space or a tab, `#` no longer opens a comment, so the + # whole tail is the plain scalar VALUE. `_rewrite_flow_line` must not + # treat a `[` inside that tail as the flow span — doing so rewrote the + # comment-shaped text and left the real value `alice` routing. + line = " reviewers: alice\u00a0# see [bob]" # NBSP, escaped on purpose + self.assertEqual( + gen._rewrite_flow_line(line, "reviewers:", ["new1", "new2"]), + " reviewers: [new1, new2]") + # A real trailing comment (space before `#`) still keeps its bytes, and a + # genuine flow value is still rewritten in place — the anchoring only + # rejects brackets that are not the value itself. + self.assertEqual( + gen._rewrite_flow_line(" reviewers: alice # see [bob]", "reviewers:", ["new1"]), + " reviewers: [new1] # see [bob]") + self.assertEqual( + gen._rewrite_flow_line(" reviewers: [a, b] # note [x]", "reviewers:", ["new1"]), + " reviewers: [new1] # note [x]") + + def test_rewriter_and_parser_agree_on_the_value_span_for_non_s_white(self): + # The rewriter located the value with a BARE `lstrip()`/`rstrip()`, which + # still absorb U+00A0 and U+001C, while `_parse_flow` trims s-white only. + # So `reviewers:[alice]` is a bare SCALAR to the parser but took the + # BRACKET branch here, emitting a line that re-parses as ONE ineligible + # scalar login: the refreshed rule routed NOBODY. Assert the round trip, + # not just the bytes — that is the property that was broken. + for value in ("\u00a0[alice]", "\u001c[alice]"): # escaped on purpose + with self.subTest(value=value): + doc = "rules:\n - paths: ['a/**']\n reviewers:%s\n" % value + _config, locs = gen.parse_reviewer_config(doc) + out = gen.rewrite_config(doc, locs, {0: ["new1", "new2"]}, None) + reparsed, _ = gen.parse_reviewer_config(out) + self.assertEqual(reparsed["rules"][0]["reviewers"], + ["new1", "new2"]) + # The mirror image at the other end: a trailing non-s-white byte is part + # of the VALUE to the parser, so the replaced span must swallow it rather + # than leave it stranded after the new list. + self.assertEqual( + gen._rewrite_flow_line(" reviewers: alice\u00a0", "reviewers:", ["new1"]), + " reviewers: [new1]") + self.assertEqual( + gen._rewrite_flow_line(" reviewers: alice\u00a0 # note", "reviewers:", ["new1"]), + " reviewers: [new1] # note") + + def test_crlf_document_round_trips_without_mixing_line_endings(self): + # `main()` reads the config as BYTES now (universal-newline translation + # would hide a CR from the parser that the runtime port plainly sees), so + # a CRLF document reaches `rewrite_config` with its CRs intact for the + # first time. Emitting bare-LF item lines into it would leave the file + # with MIXED endings — a diff on every untouched line for the reviewer of + # the drift PR. + doc = "default_pool:\r\n - alice\r\n - bob\r\n" + _config, locs = gen.parse_reviewer_config(doc) + out = gen.rewrite_config(doc, locs, {}, ["carol", "dave"]) + self.assertEqual(out, "default_pool:\r\n - carol\r\n - dave\r\n") + self.assertNotIn("\n", out.replace("\r\n", "")) + # An LF document must not acquire CRs by the same code path. + lf = "default_pool:\n - alice\n - bob\n" + _config, lf_locs = gen.parse_reviewer_config(lf) + self.assertEqual(gen.rewrite_config(lf, lf_locs, {}, ["carol"]), + "default_pool:\n - carol\n") + # The flow arm keeps the tail (CR included) on its own. + flow = "default_pool: [alice]\r\n" + _config, flow_locs = gen.parse_reviewer_config(flow) + self.assertEqual(gen.rewrite_config(flow, flow_locs, {}, ["carol"]), + "default_pool: [carol]\r\n") + def test_block_rewrite_replaces_items_at_same_indent(self): config, locs = gen.parse_reviewer_config(CONFIG) out = gen.rewrite_config(CONFIG, locs, {1: ["new-p", "new-q", "new-r"]}, None) @@ -417,6 +487,81 @@ def test_default_pool_exclude_is_case_insensitive(self): pool = gen.select_default_pool({"DrJKL": 9.0, "b": 5.0}, [], {"drjkl"}) self.assertEqual(pool, ["b"]) + def test_bom_flow_default_pool_rewrite_keeps_the_bom(self): + # `parse_reviewer_config` strips a leading U+FEFF so the first key is + # recognised; `rewrite_config` deliberately does NOT, because its + # contract is byte-faithfulness. Dropping one character off the head of + # line 0 changes no line INDEX, and `_rewrite_flow_line` locates the + # bracket span WITHIN the line, so the BOM survives the rewrite in + # place — this pins that the two halves stay compatible. + # U+FEFF stays ESCAPED here, as in parser-corpus.json: a literal would be + # invisible in review and would go silently VACUOUS if an editor or lint + # normalised it away — `startswith("")` is always true, so the BOM + # assertions below would keep passing while testing nothing. + cfg = "\ufeffdefault_pool: [old-a] # keep small\nrules:\n - paths: [\"x/**\"]\n reviewers: [r1]\n" + config, locs = gen.parse_reviewer_config(cfg) + self.assertEqual(config["default_pool"], ["old-a"]) + out = gen.rewrite_config(cfg, locs, {}, ["new-a", "new-b"]) + self.assertTrue(out.startswith("\ufeff"), "the rewrite dropped the BOM") + self.assertIn("\ufeffdefault_pool: [new-a, new-b] # keep small\n", out) + self.assertEqual(out.split("\n")[1:], cfg.split("\n")[1:]) + + +class TestDuplicateDefaultPool(unittest.TestCase): + """Last-wins plus a warning, never a rejection. + + The parsed result is pinned language-neutrally by the shared corpus; the + warning cannot be, because each port emits on its own channel (`::warning::` + here, `core.warning` in the JS step), so each side asserts its own. + """ + + def parse(self, text): + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + config, locs = gen.parse_reviewer_config(text) + return config, locs, buf.getvalue() + + def test_duplicate_warns_once_and_last_wins(self): + config, _locs, out = self.parse("default_pool: [alice]\ndefault_pool:\n - bob\n") + self.assertEqual(config["default_pool"], ["bob"]) + self.assertEqual(out.count("::warning::"), 1) + self.assertIn("duplicate top-level default_pool: key", out) + + def test_empty_first_list_still_warns(self): + # Keyed on "the key was seen", not on "the list is non-empty" — an + # emptiness test would make this port silent where the JS one warns. + _config, _locs, out = self.parse("default_pool: []\ndefault_pool: [bob]\n") + self.assertEqual(out.count("::warning::"), 1) + + def test_single_or_indented_default_pool_is_silent(self): + for text in ("default_pool: [alice]\n", + "default_pool:\n - alice\n", + "rules:\n - reviewers: [a]\n", + " default_pool: [indented]\ndefault_pool: [alice]\n"): + with self.subTest(text=text): + self.assertNotIn("::warning::", self.parse(text)[2]) + + def test_locs_still_point_at_a_rewritable_list(self): + # last-wins is a PARSE rule; the rewrite must stay coherent with it. + cfg = "default_pool: [alice]\ndefault_pool:\n - bob\n" + _config, locs, _out = self.parse(cfg) + out = gen.rewrite_config(cfg, locs, {}, ["carol"]) + self.assertEqual(out, "default_pool: [alice]\ndefault_pool:\n - carol\n") + + def test_locs_drop_a_shadowed_key_when_the_winner_has_no_items(self): + # The other half of last-wins-for-`locs`, and the dangerous one: the + # winning occurrence carries no item lines, so there is nothing to + # rewrite. `locs` must NOT fall back to the shadowed first occurrence — + # rewriting there emits a file whose refreshed pool sits on a dead key, + # re-reads as the empty winner, and regenerates the same diff forever. + cfg = "default_pool: [alice]\ndefault_pool:\nrules:\n - paths: [\"x/**\"]\n reviewers: [r1]\n" + config, locs, _out = self.parse(cfg) + self.assertEqual(config["default_pool"], []) + self.assertIsNone(locs["default_pool"]) + # With no rewritable location the document is returned untouched, rather + # than rewritten into the occurrence the parse discarded. + self.assertEqual(gen.rewrite_config(cfg, locs, {}, ["carol"]), cfg) + def _as_parser_shape(doc): """Normalize a PyYAML-loaded reviewers.yml to parse_reviewer_config's @@ -1191,7 +1336,11 @@ def test_corpus_is_non_empty(self): def test_config_cases(self): for case in self.corpus["configs"]: with self.subTest(case["name"]): - config, _locations = gen.parse_reviewer_config(case["text"]) + # stdout is swallowed only so the duplicate-key cases do not + # emit `::warning::` annotations from a passing test job; the + # warning itself is asserted by TestDuplicateDefaultPool. + with contextlib.redirect_stdout(io.StringIO()): + config, _locations = gen.parse_reviewer_config(case["text"]) self.assertEqual(config, case["expected"]) def test_glob_cases(self): diff --git a/.github/workflows/assign-reviewers.yml b/.github/workflows/assign-reviewers.yml index ea5a4c66..d5a2775b 100644 --- a/.github/workflows/assign-reviewers.yml +++ b/.github/workflows/assign-reviewers.yml @@ -28,9 +28,13 @@ name: Assign Reviewers (reusable) # reviewers: # - carol # -# Both flow (`[a, b]`) and block (`- a`) sequences are accepted. The config is -# read from the PR base SHA, so proposed routing edits cannot select their -# own owners before the edits have been reviewed. +# Both flow (`[a, b]`) and block (`- a`) sequences are accepted. A duplicate +# top-level `default_pool:` is last-wins with a warning (a repeated `rules:` +# still APPENDS, silently — do not rely on it to replace an earlier block), a +# `#` opens a comment only at the start of a line or after a space or a tab, +# and one leading BOM is tolerated. +# The config is read from the PR base SHA, so proposed routing edits cannot +# select their own owners before the edits have been reviewed. # # AUTHOR SCOPING: vars.REVIEWER_AUTHOR_ALLOWLIST, when set, restricts routing to # that list of PR authors and skips everyone else. It is the opt-in alternative to @@ -431,7 +435,13 @@ jobs: // js-yaml is not bundled with github-script; the schema is small and // fixed, so a focused parser is safer than an unavailable dependency. // PARITY-HARNESS:config-parser:begin - const parseReviewerConfig = (text) => { + // `sourcePath` is only ever used to NAME the file in a diagnostic, and is + // optional because the parity harness extracts this region on its own and + // has no path to hand it — an omitted one just drops the prefix rather than + // printing `undefined:` or hardcoding a `reviewers.yml` the caller may have + // configured away (`reviewer_config_path`). + const parseReviewerConfig = (text, sourcePath) => { + const where = sourcePath ? `${sourcePath}: ` : ''; const out = { default_pool: [], rules: [] }; const stripComment = (s) => { let inS = false, inD = false; @@ -439,12 +449,21 @@ jobs: const ch = s[i]; if (ch === "'" && !inD) inS = !inS; else if (ch === '"' && !inS) inD = !inD; - else if (ch === '#' && !inS && !inD && (i === 0 || /\s/.test(s[i - 1]))) return s.slice(0, i); + // YAML starts a comment at `#` only at column 0 or after s-white — a SPACE + // or a TAB, nothing else. `/\s/` was wider on the JS side and `isspace()` + // wider (differently) on the Python side, so the two ports disagreed on + // U+0085, U+001C and U+FEFF; both now spell out the two characters. + else if (ch === '#' && !inS && !inD && (i === 0 || s[i - 1] === ' ' || s[i - 1] === '\t')) return s.slice(0, i); } return s; }; + // YAML s-white: the ONLY characters trimmed around a token. Spelled out (not + // String.prototype.trim()) because trim() and Python strip() disagree about + // U+FEFF, U+0085 and U+001C-U+001F; the Python port's `_trim` is the same two + // characters. Corpus-pinned. + const trimSWhite = (s) => s.replace(/^[ \t]+|[ \t]+$/g, ''); const unquote = (s) => { - s = s.trim(); + s = trimSWhite(s); // `s.length >= 2` is load-bearing for PARITY — the reason that survives // being checked. A lone `"` both starts and ends with a quote, so without // the guard `slice(1, -1)` collapses that one character to the empty @@ -459,29 +478,43 @@ jobs: return s; }; const parseFlow = (s) => { - s = s.trim(); + s = trimSWhite(s); if (!s.startsWith('[')) return null; const end = s.indexOf(']'); const inner = s.slice(1, end === -1 ? s.length : end); return inner.split(',').map((x) => unquote(x)).filter((x) => x.length > 0); }; const indentOf = (l) => l.match(/^ */)[0].length; - const lines = text.split(/\r?\n/).map(stripComment); + // A single leading U+FEFF is legal YAML and must not become part of the first + // key. `trim()` used to absorb it here incidentally and the Python port's + // `strip()` did not; now that both sides trim s-white only, NEITHER absorbs it + // and this explicit single-BOM strip is the only thing removing it, on both + // sides. A SECOND BOM is data and stays — see the corpus. + const lines = text.replace(/^\uFEFF/, '').split(/\r?\n/).map(stripComment); let i = 0; + let seenDefaultPool = false; while (i < lines.length) { const raw = lines[i]; - if (!raw.trim()) { i++; continue; } - const line = raw.trim(); + if (!trimSWhite(raw)) { i++; continue; } + const line = trimSWhite(raw); if (indentOf(raw) === 0 && line.startsWith('default_pool:')) { - const rest = line.slice('default_pool:'.length).trim(); + // YAML requires unique keys; PyYAML's de-facto last-wins is the reference. + // Warn, never reject — routing must not hard-fail on a malformed map — and + // reset rather than accumulate, so the block form replaces like the flow + // form already did. The flag (not `out.default_pool.length`) is what keeps + // the warning firing for an empty first list, matching the Python port. + if (seenDefaultPool) core.warning(`${where}duplicate top-level \`default_pool:\` key — last one wins`); + seenDefaultPool = true; + const rest = trimSWhite(line.slice('default_pool:'.length)); const flow = parseFlow(rest); if (flow) { out.default_pool = flow; i++; continue; } + out.default_pool = []; i++; while (i < lines.length) { const r = lines[i]; - if (!r.trim()) { i++; continue; } + if (!trimSWhite(r)) { i++; continue; } if (indentOf(r) === 0) break; - const t = r.trim(); + const t = trimSWhite(r); if (t.startsWith('- ')) out.default_pool.push(unquote(t.slice(2))); i++; } @@ -491,10 +524,20 @@ jobs: i++; let current = null, listKey = null, ruleIndent = -1; const setKey = (seg) => { - const m = seg.match(/^(paths|reviewers):(.*)$/); + // `[^\n]*`, not `.*`: the two languages disagree about what `.` + // excludes. Python's `.` omits only LF, JS's omits LF, CR, U+2028 + // and U+2029 — so on a rule line ending in a bare CR (the last line + // of a document with no final newline, which the split above now + // preserves as data) the Python port matched and this one did NOT, + // silently dropping the key and leaving the rule with no reviewers + // while the drift generator modelled those reviewers as routing. + // Spelled out on both ports, both now keep the CR as part of the + // value — where `eligible()` rejects it, the same answer the corpus + // already pins for the `default_pool:` block arm. + const m = seg.match(/^(paths|reviewers):([^\n]*)$/); if (!m) return; const key = m[1]; - const val = m[2].trim(); + const val = trimSWhite(m[2]); const flow = parseFlow(val); if (flow) { if (current) current[key] = flow; listKey = null; } else if (val === '') { listKey = key; } @@ -502,20 +545,20 @@ jobs: }; while (i < lines.length) { const r = lines[i]; - if (!r.trim()) { i++; continue; } + if (!trimSWhite(r)) { i++; continue; } if (indentOf(r) === 0) break; const ind = indentOf(r); - const t = r.trim(); + const t = trimSWhite(r); const isDash = t === '-' || t.startsWith('- '); if (isDash && (ruleIndent === -1 || ind === ruleIndent)) { if (ruleIndent === -1) ruleIndent = ind; current = { paths: [], reviewers: [] }; out.rules.push(current); listKey = null; - const afterDash = t.slice(1).trim(); + const afterDash = trimSWhite(t.slice(1)); if (afterDash) setKey(afterDash); } else if (isDash && listKey && current) { - current[listKey].push(unquote(t.slice(1).trim())); + current[listKey].push(unquote(trimSWhite(t.slice(1)))); } else { setKey(t); } @@ -552,7 +595,7 @@ jobs: return; } const text = Buffer.from(res.data.content, 'base64').toString('utf8'); - config = parseReviewerConfig(text); + config = parseReviewerConfig(text, configPath); } catch (e) { core.info(`Could not read ${configPath}@${pr.base.sha} (${e.message}) — nothing to do.`); return; @@ -579,6 +622,41 @@ jobs: if (!candidates.has(login)) candidates.set(login, {login, configured: new Set(), history: new Map(), load: null}); return candidates.get(login); }; + // A CONFIGURED login that cannot be a GitHub login at all is config rot, and + // it is otherwise completely silent: it just never gets assigned. That became + // reachable when both ports narrowed to trimming s-white, since a login padded + // with U+00A0/U+FEFF/U+0085/U+001C or a trailing bare CR now survives parsing + // verbatim and fails the shape gate — and every one of those characters is + // invisible in an editor. Warn once per bad token so the rot is findable. + // Deliberately keyed on the SHAPE test only, not on `eligible()`: the exclude + // set legitimately holds the PR author and `EXCLUDE`, and warning about a + // correctly-configured owner who happens to have opened the PR would fire on + // essentially every run and train people to ignore it. The token is rendered + // codepoint-escaped because the whole point is that it looks fine verbatim; + // note this is NOT the `escape` declared further down (that one is an HTML + // escaper for the job summary, and `const` — referencing it from here would + // hit the temporal dead zone and throw). + const showInvisible = (s) => s.replace(/[^\x20-\x7e]/g, (c) => '\\u' + c.codePointAt(0).toString(16).padStart(4, '0')); + const warnedLogins = new Set(); + const warnConfiguredLogin = (raw) => { + const login = normalizeLogin(raw); + if (!/^[a-z\d](?:[a-z\d-]{0,38})$/i.test(login) && !warnedLogins.has(login)) { + warnedLogins.add(login); + core.warning(`${configPath ? `${configPath}: ` : ''}configured reviewer "${showInvisible(raw)}" is not a valid GitHub login and will never be assigned — check for invisible padding such as U+00A0 or a trailing CR.`); + } + }; + // WARN BROADLY, ROUTE NARROWLY. The rot is a property of the FILE, not of this + // diff, so the sweep is a lint over every configured token, run once up front and + // deliberately separate from routing. Warning from inside the candidate helper + // instead reached a rule's reviewers only once that rule's paths matched a changed + // file, and `default_pool` only on a run that fell all the way through to the + // fallback — so a rotted owner stayed silent on exactly the runs that did not touch + // its area, and a padded `default_pool` entry could go unreported for as long as any + // rule kept matching, right up until the fallback fired and needed it. This pass + // creates NO candidates: insertion below stays conditional on the routing checks, so + // an unmatched rule's owners are warned about without ever entering `candidates`. + for (const rule of config.rules) for (const login of rule.reviewers || []) warnConfiguredLogin(login); + for (const login of config.default_pool || []) warnConfiguredLogin(login); const matched = new Set(); for (const rule of config.rules) { files.forEach((file, index) => { diff --git a/docs/callers/assign-reviewers.md b/docs/callers/assign-reviewers.md index 7a6a59a0..5e2e12fd 100644 --- a/docs/callers/assign-reviewers.md +++ b/docs/callers/assign-reviewers.md @@ -189,6 +189,38 @@ rules: [This repo's own `reviewers.yml`](../../.github/reviewers.yml) is a worked example with commentary on how the buckets were seeded. +Four dialect rules the focused parser follows, shared with the `refresh-reviewers` +generator that writes this file: a **duplicate top-level `default_pool:` is last-wins** +— a second one replaces the first rather than adding to it, and the run logs a warning +rather than failing (a repeated `rules:` block is *not* covered: it still appends to the +earlier one, with no warning, so replace a rules block in place rather than restating it); +a **`#` starts a comment only at the start of a line or after a +space or a tab**, so `[x#c]` is the literal login `x#c` and only `[x #c]` is a trailing +comment; a **single leading byte-order mark is tolerated**, so a `reviewers.yml` +saved as UTF-8-with-BOM still routes; and **a login is trimmed of spaces and tabs only**, +so one padded by any other invisible character — a non-breaking space, a NEL, a *second* +byte-order mark — is taken literally as part of the login by both the runtime and the +drift generator, matches no collaborator, and will not route. Strip those characters from +the file rather than expecting either side to absorb them. + +Because that padding is invisible, the run **warns** (`configured reviewer "\u00a0alice" +is not a valid GitHub login and will never be assigned`) whenever a login configured in +`reviewers.yml` cannot be a GitHub login at all, rendering the offending token +codepoint-escaped so the character is findable. Every rule's reviewers and the whole +`default_pool` are checked on every run, including rules whose paths this PR did not +touch — the warning reports the state of the *file*, so a rotted owner does not stay +hidden until some later PR happens to change that area. Being warned about is not being +routed to: an unmatched rule's owners are still never assigned. It is deliberately silent about a +configured owner who is merely excluded — the PR author, or `vars.REVIEWER_EXCLUDE` — +since that is normal and would otherwise fire on nearly every run. + +One consequence of trimming spaces and tabs only: a line whose sole content is some +*other* invisible character is no longer a blank line, and indentation counts spaces, so +at column 0 it ends the block above it — inside `rules:` that silently discards every +rule after it. Both ports behave identically here and the corpus pins it, but a stray +non-breaking space at the start of a line is worth ruling out if owners stop being +assigned. Indented, such a line is harmless. + ## Gotchas **Dependabot PRs need the same skip as forks, for a different reason.** They are