Files
mandy/tests/test_coordination_claims.py
MoonEyes 0476fb493a Add a daily table of open claims for the coordination issue
Claim ORG-CLAIMS-TABLE (#3312).

tools/coordination_claims.py reads the comments of the coordination threads and the open pull requests and
lists every ID that was claimed and has no later done:/released: line: claimed date, ID, claimant, age in days
and up to three open pull requests naming the ID as a whole token (D3D12-A does not match D3D12-AB or
D3D12-A-2), oldest first, with counts. --update-body replaces only the text between the claims-table markers
of an issue body, appends the block when they are absent, and drops the newest rows with a "N more" line to
stay under GitHub's 65,536-character limit. No network inside the module.

.github/workflows/coordination-claims.yml runs it once a day (and on demand): it finds the newest open issue
titled "Coordinating who works on what", reads its comments and those of the thread it continues, lists the
open pull requests and edits the body with gh issue edit --body-file.

Tested with tests/test_coordination_claims.py (synthetic comments only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 11:31:26 +01:00

296 lines
14 KiB
Python

"""tools/coordination_claims.py builds the table of open claims of the coordination issue.
python -I tests/test_coordination_claims.py
Synthetic comments and pull requests only, no network and no game files. Covers the claim formats (several IDs on a
line, bold `**claiming:**`, arrows and parentheses that start prose), `done:`/`released:` closing and re-claiming, the
whole-token match of an ID in an open pull request (`D3D12-A` is not `D3D12-AB`), replacing only the text between the
markers of an issue body (and appending the block when they are absent), the cap on the body length, and that
`.github/workflows/coordination-claims.yml` runs the tool with the permissions it needs and no third-party action.
"""
import datetime
import json
import os
import re
import sys
import tempfile
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import coordination_claims as cc # noqa: E402
NOW = cc.parse_time("2026-12-03T12:00:00Z")
def comment(user, day, body, hour=9):
return {"user": user, "created_at": "2026-%sT%02d:00:00Z" % (day, hour), "body": body}
def claims(comments):
state, _ = cc.open_claims(comments)
return state
class Parsing(unittest.TestCase):
def test_one_id_and_several_ids_on_a_line(self):
state = claims([comment("ann", "11-20", "claiming: D3D12-HLE-A, VERIFY-2285 and VERIFY-7")])
self.assertEqual(sorted(state), ["D3D12-HLE-A", "VERIFY-2285", "VERIFY-7"])
self.assertEqual(state["VERIFY-2285"]["by"], "ann")
self.assertEqual(state["VERIFY-2285"]["date"], datetime.date(2026, 11, 20))
def test_bold_and_list_forms(self):
state = claims([comment("ann", "11-20", "**claiming:** AA-1\n- Claiming: BB-2\n**Claiming**: CC-3\n__claiming:__ DD-4")])
self.assertEqual(sorted(state), ["AA-1", "BB-2", "CC-3", "DD-4"])
def test_text_after_an_arrow_or_a_parenthesis_is_not_an_id_list(self):
state = claims([
comment("ann", "11-20", "claiming: AA-1 -> fix the CLI-OPTION table"),
comment("bob", "11-20", "claiming: BB-2 → see D3D12-NOT-THIS"),
comment("cy", "11-20", "claiming: CC-3 (follow-up of OLD-99)"),
])
self.assertEqual(sorted(state), ["AA-1", "BB-2", "CC-3"])
def test_the_keyword_must_start_the_line(self):
state = claims([comment("ann", "11-20", "I am not claiming: AA-1\nsee claiming: BB-2 above\nclaiming: CC-3")])
self.assertEqual(sorted(state), ["CC-3"])
def test_quotes_and_code_blocks_are_ignored(self):
text = "> claiming: QQ-1\n```\nclaiming: CC-1\n```\n~~~\nclaiming: TT-1\n~~~\nclaiming: REAL-1"
self.assertEqual(sorted(claims([comment("ann", "11-20", text)])), ["REAL-1"])
closed = claims([comment("ann", "11-20", "claiming: AA-1"),
comment("bob", "11-21", "> done: AA-1 -> #5\nnot yet")])
self.assertEqual(sorted(closed), ["AA-1"])
def test_the_first_claimant_keeps_an_open_claim(self):
state = claims([comment("bob", "11-22", "claiming: AA-1"), comment("ann", "11-20", "claiming: AA-1")])
self.assertEqual(state["AA-1"]["by"], "ann") # input order does not matter, time does
self.assertEqual(state["AA-1"]["date"], datetime.date(2026, 11, 20))
def test_not_an_id(self):
self.assertEqual(claims([comment("ann", "11-20", "claiming: the shader work, UTF8 and x-y")]), {})
def test_input_shapes(self):
one = {"user": {"login": "ann"}, "created_at": "2026-11-20T09:00:00Z", "body": "claiming: AA-1"}
two = {"u": "bob", "t": "2026-11-21T09:00:00Z", "b": "claiming: BB-2"}
jsonl = json.dumps(one) + "\n" + json.dumps(two) + "\n"
glued = json.dumps([one]) + json.dumps([two]) # what `gh api --paginate` prints without --jq
for text in (jsonl, glued, json.dumps([one, two])):
state = claims(cc.load_json_values(text))
self.assertEqual({k: v["by"] for k, v in state.items()}, {"AA-1": "ann", "BB-2": "bob"})
self.assertEqual(cc.load_json_values(""), [])
class Closing(unittest.TestCase):
def test_done_and_released_close(self):
state = claims([
comment("ann", "11-20", "claiming: AA-1, BB-2, CC-3, DD-4"),
comment("ann", "11-21", "done: AA-1 -> #12"),
comment("bob", "11-21", "Released: BB-2"),
comment("cy", "11-22", "**done:** CC-3 → #13 (merged)"),
])
self.assertEqual(sorted(state), ["DD-4"])
def test_done_closes_every_id_named_before_the_arrow(self):
state = claims([comment("ann", "11-20", "claiming: AA-1, BB-2"), comment("ann", "11-21", "done: AA-1, BB-2 -> #4")])
self.assertEqual(state, {})
def test_a_claim_after_a_release_opens_it_again(self):
state = claims([
comment("ann", "11-20", "claiming: AA-1"),
comment("ann", "11-21", "released: AA-1"),
comment("bob", "11-25", "claiming: AA-1"),
])
self.assertEqual(state["AA-1"]["by"], "bob")
self.assertEqual(state["AA-1"]["date"], datetime.date(2026, 11, 25))
def test_a_close_before_the_claim_does_not_hide_it(self):
state = claims([comment("ann", "11-20", "done: AA-1 -> #1"), comment("bob", "11-21", "claiming: AA-1")])
self.assertEqual(sorted(state), ["AA-1"])
def test_the_count_of_ids_ever_claimed_includes_closed_ones(self):
_, ever = cc.open_claims([comment("ann", "11-20", "claiming: AA-1, BB-2"), comment("ann", "11-21", "done: AA-1")])
self.assertEqual(ever, 2)
class PullRequests(unittest.TestCase):
PRS = [
{"number": 30, "title": "D3D12-AB: other thing", "body": None},
{"number": 20, "title": "fix", "body": "Claim: `D3D12-A` in #3312"},
{"number": 10, "title": "D3D12-A", "body": ""},
{"number": 40, "title": "part of D3D12-A-2", "body": "and XD3D12-A, D3D12-A_ plus D3D12-A."},
{"number": 50, "title": "d3d12-a lowercase", "body": ""},
]
def test_whole_token_only(self):
self.assertEqual(cc.prs_naming("D3D12-A", self.PRS), [10, 20, 40]) # 40 only through the final "D3D12-A."
self.assertEqual(cc.prs_naming("D3D12-AB", self.PRS), [30])
self.assertEqual(cc.prs_naming("D3D12-A-2", self.PRS), [40])
def test_longer_id_does_not_match_a_prefix(self):
prs = [{"number": 1, "title": "D3D12-A-2 and VERIFY-12", "body": ""}]
self.assertEqual(cc.prs_naming("D3D12-A", prs), [])
self.assertEqual(cc.prs_naming("VERIFY-1", prs), [])
self.assertEqual(cc.prs_naming("VERIFY-12", prs), [1])
def test_at_most_three_lowest_first(self):
prs = [{"number": n, "title": "AA-1", "body": ""} for n in (9, 3, 7, 5, 1)]
self.assertEqual(cc.prs_naming("AA-1", prs), [1, 3, 5])
self.assertEqual(cc.prs_naming("AA-1", prs, limit=2), [1, 3])
class Table(unittest.TestCase):
def setUp(self):
self.comments = [
comment("bob", "11-25", "claiming: NEW-1"),
comment("ann", "11-20", "claiming: OLD-1, GONE-1"),
comment("ann", "11-26", "done: GONE-1 -> #3"),
]
self.prs = [{"number": 77, "title": "NEW-1 landing", "body": ""}]
def rows(self):
state, ever = cc.open_claims(self.comments)
return cc.build_rows(state, self.prs, NOW), ever
def test_oldest_first_with_age_and_prs(self):
rows, _ = self.rows()
self.assertEqual([r[1] for r in rows], ["OLD-1", "NEW-1"])
self.assertEqual([r[3] for r in rows], [13, 8])
self.assertEqual(cc.row_line(rows[0]), "| 2026-11-20 | `OLD-1` | ann | 13 | none |")
self.assertEqual(cc.row_line(rows[1]), "| 2026-11-25 | `NEW-1` | bob | 8 | #77 |")
def test_counts(self):
rows, ever = self.rows()
text, shown = cc.render_table(rows, ever, NOW)
self.assertEqual(shown, 2)
self.assertIn("3 IDs claimed in the threads read, 2 with no `done:`/`released:` line, 1 of those with no open "
"pull request naming them.", text)
self.assertIn("| Claimed | ID | By | Age (days) | Open PRs naming the ID |", text)
def test_empty(self):
text, shown = cc.render_table([], 0, NOW)
self.assertEqual(shown, 0)
self.assertIn("0 IDs claimed", text)
class UpdateBody(unittest.TestCase):
def rows(self, n=2):
comments = [comment("u%d" % i, "11-20", "claiming: ID-%d" % i, hour=i % 24) for i in range(n)]
state, ever = cc.open_claims(comments)
return cc.build_rows(state, [], NOW), ever
def test_replaces_only_between_markers(self):
rows, ever = self.rows()
body = "intro\n\n%s\nOLD TABLE\n%s\n\n## Tail\nkept\n" % (cc.START, cc.END)
new = cc.update_body(body, rows, ever, NOW)
self.assertTrue(new.startswith("intro\n\n" + cc.START + "\n"))
self.assertTrue(new.endswith(cc.END + "\n\n## Tail\nkept\n"))
self.assertNotIn("OLD TABLE", new)
self.assertIn("`ID-0`", new)
self.assertEqual(new.count(cc.START), 1)
self.assertEqual(new.count(cc.END), 1)
def test_running_twice_gives_the_same_body(self):
rows, ever = self.rows()
once = cc.update_body("intro", rows, ever, NOW)
self.assertEqual(cc.update_body(once, rows, ever, NOW), once)
def test_appends_the_block_when_the_markers_are_absent(self):
rows, ever = self.rows()
new = cc.update_body("intro\n", rows, ever, NOW)
self.assertTrue(new.startswith("intro\n\n" + cc.START + "\n"))
self.assertTrue(new.endswith("\n" + cc.END))
self.assertTrue(cc.update_body("", rows, ever, NOW).startswith(cc.START))
def test_one_marker_alone_is_refused(self):
rows, ever = self.rows()
for body in ("a\n" + cc.START + "\nb", "a\n" + cc.END + "\nb", cc.END + "\nx\n" + cc.START):
with self.assertRaises(ValueError):
cc.update_body(body, rows, ever, NOW)
def test_length_cap_truncates_the_table_and_says_how_many(self):
rows, ever = self.rows(400)
limit = 6000
body = "x" * 1000 + "\n" + cc.START + "\nold\n" + cc.END + "\ntail"
new = cc.update_body(body, rows, ever, NOW, limit=limit, safety=0)
self.assertLessEqual(len(new), limit)
self.assertTrue(new.startswith("x" * 1000))
self.assertTrue(new.endswith(cc.END + "\ntail"))
shown = new.count("| `ID-")
self.assertTrue(0 < shown < 400)
self.assertRegex(new, r"%d more claims not shown" % (400 - shown))
# the oldest rows are the ones kept
self.assertIn("`ID-0`", new)
self.assertNotIn("`ID-399`", new)
def test_default_limit_is_githubs(self):
rows, ever = self.rows(2500)
new = cc.update_body("intro", rows, ever, NOW)
self.assertLess(len(new), 65536)
self.assertGreater(len(new), 60000)
self.assertIn("more claims not shown", new)
def test_a_body_that_leaves_no_room_is_an_error(self):
rows, ever = self.rows()
with self.assertRaises(ValueError):
cc.update_body("x" * 7000, rows, ever, NOW, limit=7200, safety=0)
class CommandLine(unittest.TestCase):
def test_table_and_update_body_through_files(self):
with tempfile.TemporaryDirectory() as tmp:
def write(name, text):
path = os.path.join(tmp, name)
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.write(text)
return path
comments = write("c.jsonl", "\n".join(json.dumps(c) for c in [
comment("ann", "11-20", "claiming: AA-1, BB-2"), comment("bob", "11-21", "done: BB-2 -> #9")]))
prs = write("prs.json", json.dumps([{"number": 5, "title": "AA-1", "body": ""}]))
body = write("body.md", "head\n" + cc.START + "\nold\n" + cc.END + "\n")
out = os.path.join(tmp, "out.md")
self.assertEqual(cc.main(["--comments", comments, "--prs", prs, "--now", "2026-12-03T00:00:00Z",
"--update-body", body, "--out", out]), 0)
with open(out, encoding="utf-8", newline="") as f:
text = f.read()
self.assertIn("| 2026-11-20 | `AA-1` | ann | 13 | #5 |", text)
self.assertNotIn("BB-2", text)
self.assertNotIn("\r", text)
self.assertTrue(text.startswith("head\n"))
class Workflow(unittest.TestCase):
def setUp(self):
with open(os.path.join(ROOT, ".github", "workflows", "coordination-claims.yml"), encoding="utf-8") as f:
self.text = f.read()
with open(os.path.join(ROOT, ".github", "workflows", "ci.yml"), encoding="utf-8") as f:
self.ci = f.read()
def test_triggers_and_permissions(self):
self.assertRegex(self.text, r"(?m)^on:\n schedule:\n - cron: '[^']+'\n workflow_dispatch:")
self.assertNotIn("pull_request", self.text)
job = self.text[self.text.index("\njobs:"):]
self.assertRegex(job, r"(?m)^ permissions:\n contents: read\n issues: write\n pull-requests: read\n")
self.assertRegex(self.text, r"(?m)^permissions: \{\}$")
self.assertRegex(job, r"(?m)^ timeout-minutes: \d+$")
def test_only_the_checkout_action_pinned_like_ci(self):
uses = re.findall(r"(?m)^\s*-?\s*uses:\s*(\S+)(?:\s*#.*)?$", self.text)
self.assertEqual(len(uses), 1)
self.assertTrue(uses[0].startswith("actions/checkout@"))
pinned = re.search(r"uses: (actions/checkout@[0-9a-f]{40}) # v\S+", self.ci).group(1)
self.assertEqual(uses[0], pinned)
self.assertIn("persist-credentials: false", self.text)
self.assertIn("GH_TOKEN: ${{ github.token }}", self.text)
def test_runs_the_tool_and_edits_the_body(self):
self.assertIn("python3 -I tools/coordination_claims.py", self.text)
self.assertIn("gh issue edit", self.text)
self.assertIn("--body-file", self.text)
self.assertIn("Coordinating who works on what", self.text)
self.assertIn("continued from|continuation of", self.text)
if __name__ == "__main__":
unittest.main()