diff --git a/fork-ci/actions/pr-queue/README.md b/fork-ci/actions/pr-queue/README.md index 206018bde0354..26b7aeeb71bb7 100644 --- a/fork-ci/actions/pr-queue/README.md +++ b/fork-ci/actions/pr-queue/README.md @@ -103,13 +103,28 @@ queue never takes a reviewed pull request for an unreviewed one. Looking like a report means any of: `lens:`, `read at`, `safe to merge`, a `## ... lens` heading near the top, or two `covered:`/`checks:` lines. A comment that is no report says so on its first line, `not a lens: ...`. That clears it unless one of -its lines is a lens header or a `lens:` field with a sha. +its lines is a lens header, a lens field with a sha (`lens:`, `lens=`, `lens —` +or `lens –`, quoted or not), or a `read at` with a hex sha outside a `>` quote +(a fenced or indented block is no quote). To name a sha a lens read in such a +comment, quote that line with `>`. A comment that looks like a report but is not one valid read is a lens of its own, keyed by that comment. Posting the report again the right way adds a new read and leaves the old comment blocking, and hiding it is not enough either. The ways out: delete it, edit its first line to `not a lens: ...` with no lens -header line left in it, or post an override for the head. +header line left in it and any `read at ` quoted with `>`, or post an +override for the head. + +These shapes were measured on this repository's 677 pull request comments by +people (November 2026): 25 are flagged, before and after the `not a lens:` +rule above. Of the 25, 7 are real lens reports with a broken header (one of +them NOT SAFE), caught by the `## ... lens` heading rule (6) or a hex +`read at` (1); 13 are fix-round comments caught by the same heading rule and 5 +fix-round comments with a `lens:` or `read at` in their text, and +`not a lens:` is their way out. All 34 comments that say `safe to merge` are +valid lens reads (`Verdict: not safe to merge ...`), so the rules stay as they +are: each one that flags an ordinary comment also catches broken reports that +read the same way ([issue 76](https://github.com/scuttletoby/t3code/issues/76)). ## The queue @@ -397,14 +412,16 @@ replaced, and the replaced corpus was checked against the source copy before it was committed. A rule change here must keep the corpus green or say in its pull request why a recorded answer changes. -The port differs on purpose in four ways, and the replay checks each recorded +The port differs on purpose in five ways, and the replay checks each recorded answer that changes for its reason. The source's repository is private, so its comments carry no author association: the replay gives them `OWNER` and unedited. A read shorter than 40 hex, and a lens comment edited after posting, read nothing here (54 recorded answers). The source's `full_sha`, which matched a short read to the pull request's commits, is gone. A hidden (minimized) lens comment reads nothing: the source never read `isMinimized`, and no recorded -call has it, so no recorded answer changes for it. +call has it, so no recorded answer changes for it. A comment whose first line +is `not a lens:` and that holds a hex `read at` outside a `>` quote is a lens +that read nothing (2 recorded answers; the source cleared it). `pr_queue.py` is a port of the same project's queue, and `pr_queue.test.py` holds that project's queue tests with the port's names. What the port changed: diff --git a/fork-ci/actions/pr-queue/lens.py b/fork-ci/actions/pr-queue/lens.py index 96fb89a0c735c..600d6c9e8498b 100644 --- a/fork-ci/actions/pr-queue/lens.py +++ b/fork-ci/actions/pr-queue/lens.py @@ -38,7 +38,7 @@ # leaves the old one; hiding the old one is not enough either. # The queue posts this text, so it never holds the lens marker with its colon (no_lens_text). NO_HEADER_WAY_OUT = ('delete that comment (hiding it is not enough), or edit it to start with `not a lens` and a colon ' - 'and hold no lens header line, or') + 'and hold no lens header line, with any line that names the sha a lens read quoted with `>`, or') EDITED = 'edited after posting: post a new comment instead' # A review's edit and hidden flags, by review node id, as the caller read them with a query of its own (pr_queue # review_flags). gh pr view never fetches them for reviews but prints `includesCreatedEdit: false` anyway, so a review @@ -53,13 +53,16 @@ HIDDEN = 'hidden: post a new comment instead' SHORT = 'not a full 40-hex sha' EMBEDDED = 'holds a lens header line' # lens_problem: `not a lens:` cannot clear it +# lens_problem: below a `not a lens:` first line, a hex `read at` outside a `>` quote (issue #77) +OPT_OUT_READ = 'says on its first line that it is no lens report, but holds a hex read-at sha outside a `>` quote' # What a lens report looks like (ONE rule on normalised lines): on a plain line (outside code blocks and quotes) a # `lens:`, a `read at:` or `read at `, `safe to merge`, or in its first TOP_LINES plain lines a Markdown heading # naming a lens (LOOKS_HEADING_RE reads the raw line: normalising drops `#`), or two `covered:`/`checks:` field lines; # on ANY line, fenced, indented or quoted too, a `read at` with a hex sha, a header line or a `lens:` field with any hex # sha (loose_header). Such a comment without a valid first-line header is a lens that read nothing. So is a comment # with two or more header lines (one comment per lens). A round, fix or note comment says so on its first line, -# `not a lens: ...`; that clears it only when none of its lines is a header line or a `lens:` field with a sha. +# `not a lens: ...`; that clears it only when none of its lines is a header line or a lens field with a sha, and no line +# outside a `>` quote (a fence or an indented block is no quote) holds a hex `read at` (unquoted_lines, issue #77). LOOKS_RE = re.compile(r'\blens\s*:|\bread[\s-]*at\b\s*(?::|[0-9a-f]{7})|\bsafe\s+to\s+merge\b', re.I) NOT_A_LENS_RE = re.compile(r'not\s+a\s+lens\s*:', re.I) FIELD_LINE_RE = re.compile(r'(?:covered|checks)\s*:', re.I) @@ -134,11 +137,13 @@ def clean_inline(text, narrow=False): def norm_line(line, narrow=False): """One line as lens_problem and lens_header read it: clean_inline, then any leading quote markers, list markers (`-` `+` `•` `1.` `1)`, a task box `[x]`; `*` is gone already), heading marks and `~~~` fences stripped.""" - s, prev = clean_inline(line, narrow), None - while s != prev: - prev = s - s = re.sub(r'^\s*(?:>|[-+•](?=\s|$)|\d{1,9}[.)](?=\s|$)|#{1,6}(?=\s|$)|~{3,}|\[[ xX]\](?=\s|$))\s*', '', s) - return s.strip() + return LEADING_MARKERS_RE.sub('', clean_inline(line, narrow), count=1).strip() + + +# Every leading marker in one match (issue #72: one re.sub per marker took 0.3-0.7 s for a 64 KB line of them). The same +# answer as stripping `^\s*\s*` until nothing changes: after one, the next starts at a non-blank. +LEADING_MARKERS_RE = re.compile( + r'^\s*(?:(?:>|[-+•](?=\s|$)|\d{1,9}[.)](?=\s|$)|#{1,6}(?=\s|$)|~{3,}|\[[ xX]\](?=\s|$))\s*)*') def opens_plain(line): @@ -216,28 +221,47 @@ def plain_lines(body): return out +def unquoted_lines(body): + """The comment's lines except those of a `>` quote (at most 3 leading spaces) outside a fence: a `>` in a fenced or + indented block is code, and the line stays.""" + out, fence = [], None + for line in (body or '').splitlines(): + m = re.match(r' {0,3}(`{3,}|~{3,})', line) + if fence: + if m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence): + fence = None + elif m: + fence = m.group(1) + elif re.match(r' {0,3}>', line): + continue + out.append(line) + return out + + def header_line(line): """A normalised line that is a lens header with a hex `read at`. A `` template line is none.""" return bool(LENS_RE.match(line) and READ_AT_RE.search(line)) def loose_header(line): - """A normalised line with a `lens:` field (at its start or after other text) naming any hex sha after it, however - the read is written (`read at commit X`, `reviewed at: X`, `read_at: X`, a commit URL, `(X)`). Never a valid - read; lens_problem flags it.""" - m = re.search(r'\blens\s*:(.*)', re.sub(r'\bnot\s+a\s+lens\s*:', ' ', line, flags=re.I), re.I) # opt-out: no field + """A normalised line with a lens field (at its start or after other text) naming any hex sha after it, however + the read is written (`read at commit X`, `reviewed at: X`, `read_at: X`, a commit URL, `(X)`). The field is + `lens` and a colon, or an `=`, `—` or `–` (issue #77; never `-`: no_lens_text writes `lens -`). Never a valid read; + lens_problem flags it.""" + m = re.search(r'\blens\s*[:=—–](.*)', re.sub(r'\bnot\s+a\s+lens\s*:', ' ', line, flags=re.I), re.I) # opt-out: no field return bool(m and HEX_RE.search(m.group(1))) def no_lens_text(text, keep_markdown=False): """Text that can never count as a lens read, name a lens or look like a lens report (lens_problem), for anything - the queue quotes into a comment (gh errors): no `read at`, no `lens:`, no `safe to merge`. keep_markdown: the + the queue quotes into a comment (gh errors): no `read at`, no lens field (`lens:`, `lens=`, `lens —`, `lens –`), + no `safe to merge`. keep_markdown: the queue's own advice (a command in backticks, a `` placeholder) stays as written when, read without its markdown, it holds none of these; otherwise it is cleaned like any other text.""" plain = clean_inline(text) out = re.sub(r'\bread([\s-]*)at\b', r'read\1@', plain, flags=re.I) out = re.sub(r'\bsafe(\s+)to(\s+)merge\b', r'safe\1to\2land', out, flags=re.I) - out = re.sub(r'\blens(\s*):', r'lens\1-', out, flags=re.I) + out = re.sub(r'\blens(\s*)[:=—–]', r'lens\1-', out, flags=re.I) # every field form loose_header reads return text if keep_markdown and out == plain else out @@ -346,6 +370,8 @@ def norm(line): if any(loose_header(line) for line in normed): return f'{EMBEDDED} (a lens line with a sha not written `read at: `), {start}' if opens_plain(first) and NOT_A_LENS_RE.match(norm(first)): + if any(READ_AT_RE.search(norm(line)) for line in unquoted_lines(body)): + return f'{OPT_OUT_READ}, {start}' return None plain = plain_lines(body) if sum(bool(FIELD_LINE_RE.match(norm(line))) for line in plain) >= 2: # fields of a headerless report diff --git a/fork-ci/actions/pr-queue/lens.test.py b/fork-ci/actions/pr-queue/lens.test.py index f3b93341cf147..70d790c9c7fad 100644 --- a/fork-ci/actions/pr-queue/lens.test.py +++ b/fork-ci/actions/pr-queue/lens.test.py @@ -67,7 +67,7 @@ def test_every_recorded_call_gives_the_source_answer(self): 'override_sha': SOURCE.override_sha, 'lens_header': lens.lens_header, 'no_lens_text': lens.no_lens_text, 'latest_checks': lens.latest_checks, 'check_state': lens.check_state, 'lens_key': lens.lens_key} self.assertEqual({c['fn'] for c in cases}, set(fns) | {'full_sha'}) - wrong, changed = [], 0 + wrong, changed, opt_out = [], 0, 0 for c in (c for c in cases if c['fn'] in fns): got = json.loads(json.dumps(fns[c['fn']](*c['args']))) if got == c['out']: @@ -78,9 +78,13 @@ def test_every_recorded_call_gives_the_source_answer(self): if cause and got[:2] == [True, None] and any(w in got[2] for w in CHANGED): changed += 1 continue + if c['fn'] == 'lens_problem' and c['out'] is None and (got or '').startswith(lens.OPT_OUT_READ): + opt_out += 1 # issue #77: a hex `read at` below `not a lens:` blocks now + continue wrong.append(f'{c["fn"]}{json.dumps(c["args"])[:160]}: got {got!r}, source {c["out"]!r}') self.assertEqual(wrong, [], f'{len(wrong)} of {len(cases)} differ; first: {wrong[:3]}') self.assertEqual(changed, 54, 'answers changed on purpose (a short read or an edited lens comment)') + self.assertEqual(opt_out, 2, 'answers changed on purpose (a hex read at below not a lens:, issue #77)') class Gaps(unittest.TestCase): @@ -95,6 +99,66 @@ def test_a_hex_word_without_a_digit_is_no_sha(self): self.assertIn(lens.EMBEDDED, SOURCE.lens_problem(post('Notes\n```\nlens: effaced1\n```'))) +class NotALens(unittest.TestCase): + """Issue #77: below a `not a lens:` first line, a lens read written another way (no colon after `lens`, another + field spelling) blocks too; only a `>` quote keeps such a line from counting.""" + HEX = '7838aaec7c3cb4b6deaafa3d8cd701dd6f737f9a' + + def problem(self, body): + return SOURCE.lens_problem(post(body)) + + def test_a_lens_line_without_the_colon_below_the_opt_out_blocks(self): + for read in (self.HEX, self.HEX[:10], 'abc1234'): + for line in (f'lens correctness, read at {read}', f'Lens — correctness · read at: {read}', + f'lens=correctness read at {read}', f'read at: {read}', f'the round the lens read at {read}'): + for body in (f'not a lens: round 2 fixes\n{line}', f'not a lens: notes\n\n```\n{line}\n```', + f'not a lens: notes\n {line}', f'not a lens: {line}'): + with self.subTest(body=body): + self.assertIsNotNone(self.problem(body)) + + def test_another_field_spelling_with_a_sha_blocks_with_or_without_the_opt_out(self): + for line in (f'Lens — correctness ({self.HEX[:10]})', f'lens= correctness {self.HEX}', + f'lens – correctness at {self.HEX[:7]}'): + for body in (f'not a lens: notes\n{line}', f'Notes\n{line}'): + with self.subTest(body=body): + self.assertIn(lens.EMBEDDED, self.problem(body)) + + def test_a_quoted_read_or_one_without_a_sha_stays_clear(self): + for body in (f'not a lens: round 2 fixes\n> lens correctness, read at {self.HEX}', + f'not a lens: notes\n>> correctness, read at: {self.HEX}', + 'not a lens: notes\nthe correctness lens read at the old head; read at: is the format', + 'not a lens: fix round for the lens findings\nLens — correctness, see above', + f'not a lens: fixed in {self.HEX}\nlens-check passed'): + with self.subTest(body=body): + self.assertIsNone(self.problem(body)) + + def test_a_quoted_lens_field_with_a_sha_blocks_in_any_spelling(self): # as `lens:` did before: EMBEDDED + for field in ('lens:', 'Lens —', 'lens='): + with self.subTest(field=field): + self.assertIn(lens.EMBEDDED, self.problem(f'not a lens: notes\n> {field} correctness at {self.HEX}')) + + def test_a_quote_marker_inside_a_fence_is_code_not_a_quote(self): + self.assertIsNotNone(self.problem(f'not a lens: notes\n```\n> read at {self.HEX}\n```')) + + def test_a_read_in_inline_code_below_the_opt_out_blocks(self): # the line is read cleaned, not raw + self.assertIsNotNone(self.problem(f'not a lens: notes\nread at: `{self.HEX}`')) + + def test_a_quote_marker_indented_four_spaces_is_code_not_a_quote(self): + self.assertIsNotNone(self.problem(f'not a lens: notes\n\n > read at {self.HEX}')) + + def test_the_queues_quote_of_any_lens_field_never_reads_as_a_lens_line(self): # l283-state: leave_off quotes it + for line in (f'Lens — security ({self.HEX[:10]}) NOT SAFE', f'lens= security {self.HEX}', + f'lens – security at {self.HEX[:7]}', f'lens: security read at: {self.HEX}'): + with self.subTest(line=line): + quoted = lens.no_lens_text(line) + self.assertFalse(lens.loose_header(lens.norm_line(quoted)), quoted) + self.assertIsNone(self.problem(f'Auto-merge OFF: {quoted}'), quoted) + + def test_the_way_out_names_the_quote_and_never_reads_as_a_report(self): + self.assertIn('`>`', lens.NO_HEADER_WAY_OUT) + self.assertEqual(lens.no_lens_text(lens.NO_HEADER_WAY_OUT, keep_markdown=True), lens.NO_HEADER_WAY_OUT) + + class Markdown(unittest.TestCase): """clean_inline keeps the source's answers (`]*>` tags, `!?\\[([^\\]]*)\\]\\([^)]*\\)` links) in linear time.""" @@ -279,6 +343,34 @@ def test_a_trusted_authors_long_blank_or_marker_runs_cost_no_time(self): # r4 self.head(post(body)) self.assertLess(time.monotonic() - start, 5, body[:20]) + def test_a_trusted_authors_runs_of_leading_markers_cost_no_time(self): # issue #72: 0.3-0.7 s each before + import time + shapes = ('> ' * 32768, '>' * 65536, '- ' * 32768, '+ ' * 32768, '1. ' * 21845, '[x] ' * 16384, '# ' * 32768, + '~~~ ' * 16384, '> - 1. # [x] ' * 5000 + f'lens: a · read at: {SHA_A}') + for body in shapes: # end to end: generous, the rest is unchanged code and a busy host is slow + start = time.monotonic() + self.head(post(body)) + self.assertLess(time.monotonic() - start, 5, body[:20]) + start = time.monotonic() # norm_line alone: 5-13 ms per shape; the old loop took 0.3-0.7 s each + for body in shapes: + lens.norm_line(body) + self.assertLess(time.monotonic() - start, 0.5) + + def test_one_pass_strips_the_markers_the_old_loop_stripped(self): # issue #72: same answers, one pass + def loop(line): # the source's norm_line: one leading marker per pass + s, prev = lens.clean_inline(line), None + while s != prev: + prev = s + s = re.sub(r'^\s*(?:>|[-+•](?=\s|$)|\d{1,9}[.)](?=\s|$)|#{1,6}(?=\s|$)|~{3,}|\[[ xX]\](?=\s|$))\s*', '', s) + return s.strip() + import random + rnd = random.Random(72) + parts = ('>', '> ', '-', '- ', '+ ', '• ', '1.', '1. ', '12) ', '1234567890. ', '#', '# ', '####### ', '~~', '~~~', + '~~~~ ', '[x] ', '[ ]', '[X]x', ' ', '\t', 'lens: a', 'read at ', 'x', '-x', '#x', '*', '`') + lines = [''.join(rnd.choice(parts) for _ in range(rnd.randint(0, 12))) for _ in range(3000)] + lines += ['~' * 65536 + 'x', '> ' * 1000 + '1.x', '#' * 7 + ' lens: a'] + self.assertEqual([lens.norm_line(x) for x in lines], [loop(x) for x in lines]) + def test_many_capped_stranger_posts_cost_no_time(self): # r4 sec: 0.76 s per 4096-char post before import time body = 'lens: a' + ' ' * (lens.STRANGER_SCAN - 9) + 'b' diff --git a/fork-ci/actions/pr-queue/pr_queue.test.py b/fork-ci/actions/pr-queue/pr_queue.test.py index 8a4b7558e0a34..87703822331df 100644 --- a/fork-ci/actions/pr-queue/pr_queue.test.py +++ b/fork-ci/actions/pr-queue/pr_queue.test.py @@ -250,9 +250,18 @@ def inline_node(body, when='2026-11-07T10:00:00Z', login='owner', association='O 'authorAssociation': association, 'author': {'__typename': typename, 'login': login}} +# GraphQL's PageInfo.endCursor is a nullable String: GitHub can say hasNextPage true and give no cursor. `more=NO_CURSOR` +# fakes that page (more=None is the last page). +NO_CURSOR = object() + + +def page_info(more): + return {'hasNextPage': more is not None, 'endCursor': None if more is NO_CURSOR else more} + + def inline_page(nodes=(), more=None): - """One page of a review's inline comments; more: the endCursor of a next page.""" - return {'pageInfo': {'hasNextPage': more is not None, 'endCursor': more}, 'nodes': list(nodes)} + """One page of a review's inline comments; more: the endCursor of a next page (NO_CURSOR: there is one, no cursor).""" + return {'pageInfo': page_info(more), 'nodes': list(nodes)} def flags_page(*reviews, edited=False, hidden=False, more=None, typename='User', inline=(), inline_more=None): @@ -264,7 +273,7 @@ def flags_page(*reviews, edited=False, hidden=False, more=None, typename='User', if inline or inline_more: nodes.append({'id': 'PRR_inline', 'includesCreatedEdit': False, 'isMinimized': False, 'url': 'https://x/PRR_inline', 'author': {'__typename': 'User', 'login': 'owner'}, 'comments': inline_page(inline, inline_more)}) - page = {'hasNextPage': more is not None, 'endCursor': more} + page = page_info(more) return ok(json.dumps({'data': {'repository': {'pullRequest': {'reviews': {'pageInfo': page, 'nodes': nodes}}}}})) @@ -590,6 +599,14 @@ def test_a_next_page_without_a_new_cursor_fails(self): (flags, why), _ = self.read(flags_page(r, more='')) self.assertIsNone(flags) + def test_a_next_page_with_a_null_cursor_fails(self): # endCursor is nullable: never read as the last page + r = gh_review('x', 't') + for pages in ([flags_page(r, more=NO_CURSOR)], [flags_page(r, more='c1'), flags_page(r, more=NO_CURSOR)]): + (flags, why), calls = self.read(*pages) + self.assertIsNone(flags, len(pages)) + self.assertIn('no next page', why) + self.assertEqual(len(calls), len(pages)) + def test_more_pages_than_the_limit_fail(self): r = gh_review('x', 't') pages = [flags_page(r, more=f'c{i}') for i in range(Q.REVIEW_PAGES + 1)] @@ -2404,6 +2421,15 @@ def test_a_next_page_of_inline_comments_without_a_new_cursor_fails(self): (flags, why), _ = self.read(flags_page(inline=[inline_node('a')], inline_more='')) self.assertIsNone(flags) + def test_a_next_page_of_inline_comments_with_a_null_cursor_fails(self): # endCursor is nullable + for pages in ([flags_page(inline=[inline_node('a')], inline_more=NO_CURSOR)], + [flags_page(inline=[inline_node('a')], inline_more='i1'), + more_comments_page([inline_node('b')], more=NO_CURSOR)]): + (flags, why), calls = self.read(*pages) + self.assertIsNone(flags, len(pages)) + self.assertIn('no next page', why) + self.assertEqual(len(calls), len(pages)) + def test_more_pages_than_the_limit_fail(self): pages = [flags_page(inline=[inline_node('a')], inline_more='i0')] + \ [more_comments_page([inline_node('b')], more=f'i{i}') for i in range(1, Q.REVIEW_PAGES + 1)]