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
61 changes: 61 additions & 0 deletions .github/script/ci/test_devkit_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,67 @@ def test_real_maps_load(self):
self.assertLess(max(footprints), 2.0, res["models"])


def bench_comment_sanitize():
"""sanitize() from devkit-bench-comment.yml's inline script (the
workflow checks nothing out, so the code lives there): the part above
its "--- main" line."""
text = (REPO / ".github/workflows/devkit-bench-comment.yml").read_text(encoding="utf-8")
block = text.split("python3 - <<'EOF'\n", 1)[1].split("\n EOF\n", 1)[0]
lines = [l[10:] if l.startswith(" " * 10) else l.strip() for l in block.splitlines()]
code = "\n".join(lines).split("# --- main", 1)[0]
namespace: dict = {}
exec(compile(code, "devkit-bench-comment.yml", "exec"), namespace) # noqa: S102 - our own workflow's code
return namespace["sanitize"]


class BenchComment(unittest.TestCase):
"""The PR comment renders the summary as Markdown (its tables), with
everything that acts taken out: the summary is an artifact a pull
request's code writes."""

sanitize = staticmethod(bench_comment_sanitize())

def test_tables_headings_and_code_render_as_they_are(self):
summary = ("## Dev-kit bench (pr) on `1e02187951`\n\n✅ No hard-gate failure.\n\n### Linux XRT\n\n"
"| Model | Machine | Context | Decode base → dev-kit | Δ |\n|---|---|---|---|---|\n"
"| qwen3:0.6b | ryzenai-npu4 | 1k | 20.0 → 18.0 | -10.0% |\n\n- Linux XRT qwen3:0.6b: decode -10.0%\n")
self.assertEqual(self.sanitize(summary), summary)

def test_nothing_acts(self):
zw = "\u200b"
cases = {
"</details><!-- devkit-bench-comment -->": "&lt;/details&gt;&lt;!-- devkit-bench-comment --&gt;",
'<img src="https://x/t.png">': '&lt;img src="https:/' + zw + '/x/t.png"&gt;',
"[click](https://evil.example)": "\\[click\\](https:/" + zw + "/evil.example)",
# An input backslash must not cancel the added one.
"\\[x\\](//evil.example)": "\\\\\\[x\\\\\\](/" + zw + "/evil.example)",
"github.com/o/r/pull/1 and /issues/4": "github" + zw + ".com/o/r/pull/" + zw + "1 and /issues/" + zw + "4",
"![pixel](http://t/x.gif)": "!\\[pixel\\](http:/" + zw + "/t/x.gif)",
"[ref]: https://evil.example": "\\[ref\\]: https:/" + zw + "/evil.example",
"see www.evil.example": "see www" + zw + ".evil.example",
"cc @someone @org/team": "cc @" + zw + "someone @" + zw + "org/team",
"fixes #12 and GH-34, gh-5": "fixes #" + zw + "12 and GH-" + zw + "34, gh-" + zw + "5",
"a & b": "a &amp; b",
"safe\u202eevil\x07": "safeevil",
}
for raw, want in cases.items():
with self.subTest(raw=raw):
self.assertEqual(self.sanitize(raw), want)
# A heading's # stays a heading.
self.assertEqual(self.sanitize("### Linux XRT"), "### Linux XRT")

def test_long_summary_is_cut_at_a_line(self):
out = self.sanitize("| a | b |\n" * 10000)
self.assertLessEqual(len(out), 60000 + 100)
self.assertTrue(out.endswith("(cut: see the run's bench-summary artifact)"))
self.assertIn("| a | b |\n\n(cut", out)

def test_workflow_renders_markdown_not_a_code_block(self):
text = (REPO / ".github/workflows/devkit-bench-comment.yml").read_text(encoding="utf-8")
self.assertIn("text = sanitize(raw.decode(", text)
self.assertNotIn("{fence}text", text)


def run(**kw):
base = {"id": 5, "run_number": 3, "run_attempt": 1, "path": resolve.BUILD_WORKFLOW,
"repository": {"full_name": "ROCm/FastFlowLM"}, "head_repository": {"full_name": "ROCm/FastFlowLM"},
Expand Down
47 changes: 39 additions & 8 deletions .github/workflows/devkit-bench-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ name: Dev-kit Bench comment
# commit and head repository; never from the artifact
# - the artifact: only bench-summary, at most 64 KB as listed, a zip of
# exactly summary.md (no links or other entries), at most 60000 bytes
# read; @ is defused so nothing mentions anyone, and the text goes in a
# code fence longer than any backtick run in it, inside <details>, so it
# cannot close the fence, forge the header or hide a marker
# read; it is rendered as Markdown (its tables), inside <details>, after
# sanitize() takes out everything that acts: < > & are escaped (no HTML,
# so it cannot close the <details>, forge the header or hide a marker),
# backslashes and [ ] are escaped (no link or image), every // and
# www. and github.com, @ mentions and #123 / GH-123 / .../issues/123
# references are broken with a zero-width space, control and bidi
# characters are dropped (test_devkit_bench.py BenchComment)
# - the comment to update is the newest by github-actions[bot] starting
# with the marker; otherwise a new one
# - every event field reaches the scripts through env:
Expand Down Expand Up @@ -118,19 +122,46 @@ jobs:
set -euo pipefail
python3 - <<'EOF'
import json, os, re

ZWSP = "\u200b"
# Control and bidi characters (no hidden or reordered text).
INVISIBLE = re.compile("[\x00-\x08\x0b-\x1f\x7f\u200e\u200f\u202a-\u202e\u2066-\u2069]")
LIMIT = 60000

def sanitize(text):
"""The bench summary as Markdown that renders its headings,
tables, lists and inline code, and nothing that acts: no HTML
(so no element, comment or closing tag of this comment's own),
no link, image or bare URL, no mention or issue reference."""
text = INVISIBLE.sub("", text.replace("\r", ""))
# Backslashes first: an input \ must not pair with one added
# below and leave a [ live.
text = text.replace("\\", "\\\\")
text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
text = text.replace("[", "\\[").replace("]", "\\]")
# Every //, not only ://: no URL of any scheme, nor //host.
text = text.replace("//", "/" + ZWSP + "/").replace("www.", "www" + ZWSP + ".")
text = re.sub(r"(?i)github\.com", "github" + ZWSP + ".com", text)
text = text.replace("@", "@" + ZWSP)
# Issue and pull request references: #12, GH-12, .../issues/12.
text = re.sub(r"#(?=\d)", "#" + ZWSP, text)
text = re.sub(r"(?i)\b(GH)-(?=\d)", r"\1-" + ZWSP, text)
text = re.sub(r"(?i)/(issues|pulls?|discussions)/(?=\d)", r"/\1/" + ZWSP, text)
if len(text) > LIMIT:
text = text[:LIMIT].rsplit("\n", 1)[0] + "\n\n(cut: see the run's bench-summary artifact)"
return text

# --- main (test_devkit_bench.py runs only what is above) ---
e = os.environ
raw = open(os.path.join(e["RUNNER_TEMP"], "summary", "summary.md"), "rb").read()
text = raw.decode("utf-8", errors="replace").replace("\r", "")
# No mentions or team pings: a zero-width space after every @.
text = text.replace("@", "@\u200b")
fence = "`" * max(3, 1 + max((len(m) for m in re.findall(r"`+", text)), default=0))
text = sanitize(raw.decode("utf-8", errors="replace"))
run_id = re.sub(r"\D", "", e["RUN_ID"])
sha = re.sub(r"[^0-9a-f]", "", e["HEAD_SHA"])[:10]
url = f"https://github.com/{e['GITHUB_REPOSITORY']}/actions/runs/{run_id}"
body = (f"{e['MARKER']}\n### Dev-kit bench\n\n"
f"Results of the dev-kit benches in [Build and Test run {run_id}]({url}) for `{sha}`, as that run reported "
f"them (its artifacts hold the per-model reports).\n\n"
f"<details open><summary>Summary</summary>\n\n{fence}text\n{text}\n{fence}\n\n</details>\n")
f"<details open><summary>Summary</summary>\n\n{text}\n\n</details>\n")
with open(os.path.join(e["RUNNER_TEMP"], "body.json"), "w", encoding="utf-8") as f:
json.dump({"body": body}, f)
EOF
Expand Down
Loading