Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions fork-ci/actions/pr-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sha>` 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

Expand Down Expand Up @@ -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:
Expand Down
52 changes: 39 additions & 13 deletions fork-ci/actions/pr-queue/lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <hex>`, `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)
Expand Down Expand Up @@ -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*<marker>\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):
Expand Down Expand Up @@ -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 `<sha>` 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 `<sha>` 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


Expand Down Expand Up @@ -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: <sha>`), {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
Expand Down
94 changes: 93 additions & 1 deletion fork-ci/actions/pr-queue/lens.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand All @@ -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):
Expand All @@ -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: <sha> 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 (`</?[A-Za-z][^>]*>` tags, `!?\\[([^\\]]*)\\]\\([^)]*\\)` links) in linear time."""

Expand Down Expand Up @@ -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'
Expand Down
Loading
Loading