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
116 changes: 110 additions & 6 deletions .github/scripts/post_claude_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
marker is how PR Auto-Merge and the skill's landing decision tell it apart
from anything else posted under that login.

A comment may carry one GitHub suggestion, a fenced block the reader applies to the commented
line with one click, and only a single line (vet_suggestions): any other is turned into a
plain text fence. So is every suggestion in a comment moved to the nearest diff line, which
would replace the wrong line, and in a body finding, where none can be applied. A body
finding is indented under its bullet so its paragraphs stay with it.

Standard library only, like release_post.py; the gh CLI does the rest.
"""

Expand All @@ -46,6 +52,7 @@
import re
import subprocess
import sys
import unicodedata
import urllib.parse

# Accepted limit (repo-ci-baseline's ADR 0004): the marker proves the login,
Expand All @@ -72,6 +79,58 @@
)
REDACTED = "[redacted]"

# CREDENTIAL scans from every `eyJ`, and a long run of token characters (the first segment of a JWT
# holds the letters e, y and J) is rescanned from each one: quadratic on text a diff can steer.
# So a token, with the dots joining its segments, that holds a run longer than MAX_TOKEN_RUN is
# replaced whole before CREDENTIAL looks at the text: no legitimate review text has one, and it
# leaves nothing of a long JWT (its signature included) behind. What is left has runs of at most
# MAX_TOKEN_RUN characters, which CREDENTIAL scans in time linear in the text.
MAX_TOKEN_RUN = 128
TOKEN_CHAIN = re.compile(r"[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*")


def redact_credentials(text):
"""The text with credential-shaped text replaced, and how many were replaced."""
count = 0

def long_chain(match):
nonlocal count
chain = match.group()
if any(len(run) > MAX_TOKEN_RUN for run in chain.split(".")):
count += 1
return REDACTED
return chain

text = TOKEN_CHAIN.sub(long_chain, text)
text, found = CREDENTIAL.subn(REDACTED, text)
return text, count + found


MAX_PATH = 1024


def redact_path(path):
"""A finding's path with credential-shaped text replaced, and how many were replaced.

Not the long-run rule: a file name is one token, and a long one (a generated test's name, a
hash-named fixture) is no credential, while a path that no longer matches the diff leaves
its finding outside it. A path past MAX_PATH is no real path and gets the full rule.
"""
if len(path) > MAX_PATH:
return redact_credentials(path)
return CREDENTIAL.subn(REDACTED, path)

# A fence whose info string starts with `suggestion`, anywhere in a body (after a list or quote
# marker, or a lone CR, too): GitHub shows its content as a change the reader can apply with
# one click to the line the comment sits on, and the legacy `suggestion:-1+1` form changes more.
# Any whitespace but a line break may sit between the fence and the info string.
# A match starts only at the start of a run of fence characters, so a long run costs a linear scan.
SUGGESTION_FENCE = re.compile(r"(?i)((?<!`)`{3,}|(?<!~)~{3,})[^\S\r\n]*suggestion\b")
# The one form a comment may keep: a fence on its own lines whose info string is exactly
# `suggestion`, holding one non-empty line. The leading class and \S are disjoint, so
# matching is linear.
ONE_LINE_SUGGESTION = re.compile(r"(?m)^(`{3,}|~{3,})suggestion[ \t]*\n([^\S\n\r]*\S[^\n\r]*)\n\1[ \t]*$")


class MalformedFindings(ValueError):
pass
Expand Down Expand Up @@ -161,16 +220,57 @@ def diff_lines(files):
def redact(summary, findings):
"""The summary and findings with credential-shaped text replaced, and how many were replaced."""
count = 0
def clean(text):
def clean(text, scan=redact_credentials):
nonlocal count
text, n = CREDENTIAL.subn(REDACTED, text)
text, n = scan(text)
count += n
return text
summary = clean(summary)
findings = [{**f, "path": clean(f["path"]), "body": clean(f["body"])} for f in findings]
findings = [{**f, "path": clean(f["path"], redact_path), "body": clean(f["body"])} for f in findings]
return summary, findings, count


def defuse_suggestions(body):
"""The body with each suggestion fence turned into a plain text fence, so no one-click suggestion applies."""
return SUGGESTION_FENCE.sub(r"\1text", body)


# Characters that render blank but are letters or symbols (Hangul fillers, the blank Braille cell,
# Khmer inherent vowels), which \s does not match.
BLANK_FILLERS = "\u115f\u1160\u17b4\u17b5\u2800\u3164\uffa0"


def suggestion_line_is_safe(fence, line):
"""Whether `line` can be the one line of a suggestion that `fence` opened.

Not when it would close the block (a suggestion with no content deletes the line), nor when
it holds a character the reader can't see in the diff: a control or format character
(zero-width, bidirectional) or a Unicode line or paragraph separator, or when nothing in it is
visible (spaces, combining marks and the blank fillers only).
"""
closes = re.fullmatch(" {0,3}" + re.escape(fence[0]) + "{" + str(len(fence)) + ",}[ \t]*", line)
hidden = any(unicodedata.category(c) in ("Cf", "Zl", "Zp") or (unicodedata.category(c) == "Cc" and c != "\t") for c in line)
blank = all(c in BLANK_FILLERS or unicodedata.category(c) in ("Zs", "Cc", "Mn") for c in line)
return not closes and not hidden and not blank


def vet_suggestions(body):
"""The body as it is when it holds no suggestion, or exactly one well-formed single-line suggestion.

Any other suggestion (several, several lines, the multi-line form, an empty or hidden one, text
redacted from a credential) is defused: the prompt asks for one line only, but a suggestion is
applied with one click and what the model writes comes from a PR it read, so the script holds
the rule.
"""
fences = SUGGESTION_FENCE.findall(body)
if not fences:
return body
block = ONE_LINE_SUGGESTION.search(body)
if len(fences) == 1 and block and REDACTED not in block.group(2) and suggestion_line_is_safe(block.group(1), block.group(2)):
return body
return defuse_suggestions(body)


def build_review(summary, findings, commentable, head, line_count=lambda path: None):
"""The review to post: inline comments for findings on the diff, the rest in the body.

Expand All @@ -182,14 +282,18 @@ def build_review(summary, findings, commentable, head, line_count=lambda path: N
path, line, body = finding["path"], finding["line"], finding["body"]
lines = commentable.get(path, ())
if line in lines:
comments.append({"path": path, "line": line, "side": "RIGHT", "body": body})
comments.append({"path": path, "line": line, "side": "RIGHT", "body": vet_suggestions(body)})
elif lines and (line < 1 or (length := line_count(path)) is not None and line > length):
# Not a line of the file: Claude got the number wrong.
nearest = min(lines, key=lambda n: (abs(n - line), n))
note = f"_Claude cited line {line}, which isn't a line of this file, so this is on the nearest line in the diff._"
comments.append({"path": path, "line": nearest, "side": "RIGHT", "body": f"{note}\n\n{body}"})
# A suggestion would replace the nearest line, not the one Claude meant.
comments.append({"path": path, "line": nearest, "side": "RIGHT", "body": f"{note}\n\n{defuse_suggestions(body)}"})
else:
off_diff.append(f"- `{path}:{line}`: {body}")
# In the review body a suggestion can't be applied, and a body of several
# paragraphs stays under its bullet when indented.
indented = defuse_suggestions(body).replace("\n", "\n ")
off_diff.append(f"- `{path}:{line}`: {indented}")

parts = ["**Claude Review**", summary.strip()]
if not findings:
Expand Down
14 changes: 11 additions & 3 deletions .github/scripts/release_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def _api(self, path, jq=None):
return subprocess.run(command, check=True, capture_output=True, text=True).stdout

def _list(self, path, jq=".[] | @json"):
return [json.loads(line) for line in self._api(path, jq).splitlines() if line.strip()]
# Split on newlines only: jq's @json leaves U+2028 and U+2029 unescaped, and
# splitlines() would cut a record at either one.
return [json.loads(line) for line in self._api(path, jq).split("\n") if line.strip()]

def pull(self, number):
return json.loads(self._api(f"pulls/{number}"))
Expand Down Expand Up @@ -98,7 +100,11 @@ def version_key(tag):

def commit_subject(commit):
message = (commit.get("commit") or {}).get("message") or ""
return message.splitlines()[0].strip() if message.strip() else "(no message)"
# Cut at Markdown's line endings (CRLF, CR, LF) and nothing else: a CR left in the
# subject would start a new Markdown line, while splitlines() would also cut a
# subject at a U+2028 or U+2029 that Markdown reads as text.
first = re.split(r"\r\n|\r|\n", message, maxsplit=1)[0].strip()
return first if message.strip() else "(no message)"


# A commit subject that starts a list item this way, or after the quote or list
Expand Down Expand Up @@ -1334,7 +1340,9 @@ def update_blog_index(blog_dir, merged_date, title_line, post_name):
# Keep the post rows, skipping the header and its separator. Matching the
# header's first cell exactly keeps posts whose title mentions "Date".
rows = []
for line in existing.splitlines():
# Cut at Markdown's line endings only: splitlines() would cut a row at a U+2028 or
# U+2029 in a title, and neither half would be kept.
for line in re.split(r"\r\n|\r|\n", existing):
line = line.strip()
if not (line.startswith("|") and line.endswith("|")):
continue
Expand Down
Loading
Loading