diff --git a/.github/scripts/controller.py b/.github/scripts/controller.py index 24209313..e2e7b5a9 100644 --- a/.github/scripts/controller.py +++ b/.github/scripts/controller.py @@ -82,6 +82,24 @@ def closing_refs(body: Optional[str], repo: str) -> List[int]: return sorted(out) +# A body can point at its issue without promising to close it: ``Refs #12``, +# ``Part of #12``. Those keep a claim alive (issue #1161), but they never make a +# PR an agent PR — for that only ``closing_refs`` counts. +_LINKING = re.compile(r"\b(?:refs?|part\s+of)\b:?[ \t]+((?:[\w.-]+/[\w.-]+)?#\d{1,7}" + r"(?:[ \t]*(?:,|and)[ \t]*(?:[\w.-]+/[\w.-]+)?#\d{1,7})*)", re.I) +_REF = re.compile(r"(?:([\w.-]+/[\w.-]+))?#(\d{1,7})\b") + + +def ttl_refs(body: Optional[str], repo: str) -> List[int]: + """Every issue this body points at: the closing refs plus ``Refs``/``Part of``.""" + out = set(closing_refs(body, repo)) + for m in _LINKING.finditer(str(body or "")[:20000]): + for ref in _REF.finditer(m.group(1)): + if not ref.group(1) or _same(ref.group(1), repo): + out.add(int(ref.group(2))) + return sorted(out) + + def is_agent_pr(pr: Dict[str, Any], config: Dict[str, Any], repo: str, agent_issues: Iterable[int]) -> bool: """By author, or a branch of this repository with an agent prefix, or a branch of this repository that closes an agent-ready/agent-working issue. @@ -353,7 +371,7 @@ def read_prs(api: Any, repo: str, config: Dict[str, Any], gate: "risk_gate.Gate" def linked_prs(prs: List[Dict[str, Any]], repo: str) -> Dict[int, List[Dict[str, Any]]]: linked: Dict[int, List[Dict[str, Any]]] = {} for pr in prs: - for n in closing_refs(pr["body"], repo): + for n in ttl_refs(pr["body"], repo): linked.setdefault(n, []).append({"lastPushAt": pr["lastPushAt"]}) return linked diff --git a/changelog.d/1161-claim-expiry-refs.md b/changelog.d/1161-claim-expiry-refs.md new file mode 100644 index 00000000..7293d4df --- /dev/null +++ b/changelog.d/1161-claim-expiry-refs.md @@ -0,0 +1,5 @@ +Fixed +A claim no longer expires under a pull request that links its issue with +`Refs #N` or `Part of #N`: the controller now reads those references when it +decides whether the claim's TTL is still fed by a push, while only closing +keywords still make a PR an agent PR (#1161). diff --git a/tests/test_process_controller.py b/tests/test_process_controller.py index 070d43d5..1b4384b7 100644 --- a/tests/test_process_controller.py +++ b/tests/test_process_controller.py @@ -107,6 +107,32 @@ def test_claim_expiry(): assert not c.claim_expired(fresh, [], CONFIG, NOW) +def test_a_refs_pull_request_keeps_the_claim_alive(): + claimed = issue(2, ["agent-working"], [ev("labeled", "2026-10-19T01:00:00Z", "Birfy", "agent-working")]) + for body in ("Refs #2", "refs #2", "Part of #2", "Part of ThinkFlowLab/agentdescent#2", + "Refs #2, #3"): + prs = [pr(30, body=body)] + linked = c.linked_prs(prs, REPO).get(2, []) + assert not c.claim_expired(claimed, linked, CONFIG, NOW), f"{body!r} must keep the claim alive" + foreign = c.linked_prs([pr(30, body="Refs other/repo#2")], REPO) + assert 2 not in foreign, "a foreign repository reference names another issue" + + +def test_no_reference_still_expires_the_claim(): + claimed = issue(2, ["agent-working"], [ev("labeled", "2026-10-19T01:00:00Z", "Birfy", "agent-working")]) + for body in ("", "see #2", "Refs #29", "mentions #2 in prose"): + assert c.claim_expired(claimed, c.linked_prs([pr(30, body=body)], REPO).get(2, []), CONFIG, NOW), body + stale = [pr(30, body="Refs #2", push="2026-10-19T05:00:00Z")] + assert c.claim_expired(claimed, c.linked_prs(stale, REPO).get(2, []), CONFIG, NOW), "a stale push expires" + + +def test_a_refs_pull_request_is_still_not_an_agent_pr(): + cfg = dict(CONFIG, agentAuthors=["bot-x"]) + assert not c.is_agent_pr(pr(1, body="Refs #7"), cfg, REPO, {7}) + assert not c.is_agent_pr(pr(1, body="Part of #7"), cfg, REPO, {7}) + assert c.is_agent_pr(pr(1, body="Fixes #7"), cfg, REPO, {7}), "closing refs still decide agent-PR-ness" + + def test_plan_issues(): ready_by_other = issue(3, ["agent-ready", "agent-working"], [ev("labeled", "2026-10-19T01:00:00Z", "HugoXK", "agent-ready")])