Files
betterxen/scripts/agent_queue/queue.py
elforeign 6b479a6617 agent queue: fix review findings in replies, verification and issue forms
From independent review of the queue follow-up:
- Bot replies echoed user text (/blocked reasons, /reassign logins,
  file patterns) verbatim. Markers count when the bot posts them, so a
  reason containing an approval or done marker became a bot marker and
  could approve an acceptance command or hide another user's claim.
  Echoed text now has angle brackets and backticks escaped.
- /verified accepted a 7-character prefix and ignored extra arguments;
  a commit with the same short prefix is cheap to make. Require exactly
  one full 40-character SHA, with one parser shared by decide and the
  check.
- After a verification, the job looked up the check run by the pull
  request head SHA, but pull_request_target runs carry the base branch
  SHA, so it found nothing and || true hid it. The verify job now
  evaluates the same advisory check itself and no longer needs
  actions: write.
- The issue form renders Files inside a ```text fence and the parser
  took "text" as a path, so any two form-created tasks overlapped.
  Fence lines are skipped.
- A command still unanswered after lease_hours now gets a "Not decided"
  reply (window 2 x lease_hours) instead of being dropped silently.

Each fix has a test shown to fail with the fix reverted.

Refs #210, #211

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
2026-11-24 01:05:28 -06:00

69 lines
3.9 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
"""Queue-wide views: machine-readable summary and the scheduled audit."""
from . import config as cfgmod
from . import lease as L
from . import tasks as T
from .decide import quote, task_index
from .patterns import sets_overlap
def summary(state, now_iso=""):
tasks = task_index(state)
by_area, rows = {}, []
for n in sorted(tasks):
t = tasks[n]
if t["state"] != "OPEN" or not any(l.startswith("agent:") for l in t["labels"]):
continue
st = T.task_state(t)
area = t["area"] or "unset"
by_area.setdefault(area, {}).setdefault(t["priority"], {}).setdefault(st, []).append(n)
rows.append({"number": n, "title": t["title"], "area": area, "priority": t["priority"],
"state": st, "holder": t["holder"], "depends": t["depends"], "files": t["files"]})
counts = {}
for r in rows:
counts[r["state"]] = counts.get(r["state"], 0) + 1
prs = [{"number": p["number"], "author": p["author"], "head_sha": p.get("head_sha", ""),
"links": L.linked_issues(p.get("body")), "files": len(p.get("files", []))}
for p in state.get("prs", [])]
return {"generated_at": now_iso, "totals": counts, "by_area": by_area, "tasks": rows, "pull_requests": prs}
def audit(state, cfg, now):
"""Actions that lapse stale claims and mark invalid or conflicting tasks."""
tasks = task_index(state)
actions = []
cycle_nodes = {n for cyc in T.find_cycles(tasks) for n in cyc}
claimed = [t for t in tasks.values() if t["state"] == "OPEN" and "agent:claimed" in t["labels"]]
for t in sorted(claimed, key=lambda x: x["number"]):
comments = state.get("task_comments", {}).get(str(t["number"]), [])
if "agent:blocked" in t["labels"]:
continue
if L.lapsed(t, comments, state.get("prs", []), cfg, now):
actions.append({"issue": t["number"], "add_labels": ["agent:ready"],
"remove_labels": ["agent:claimed", "claimed-by:" + t["holder"]],
"comment": "Claim lapsed after %s hours without progress; the task is back in the queue." % cfg["lease_hours"]})
lapsed_nums = {a["issue"] for a in actions}
for i, a in enumerate(claimed):
for b in claimed[i + 1:]:
hit = sets_overlap(a["files"], b["files"])
if hit:
for x, y in ((a, b), (b, a)):
if "agent:conflict" not in x["labels"] and x["number"] not in lapsed_nums:
actions.append({"issue": x["number"], "add_labels": ["agent:conflict"], "remove_labels": [],
"comment": "File overlap with claimed task #%d (`%s` vs `%s`). A maintainer must resolve it." % (y["number"], quote(hit[0]), quote(hit[1]))})
for t in sorted(tasks.values(), key=lambda x: x["number"]):
if t["state"] != "OPEN" or "agent:ready" not in t["labels"]:
continue
d = T.dep_problems(t, tasks)
if d["missing"] or d["cycle"] or t["number"] in cycle_nodes:
why = ("missing dependencies %s" % d["missing"]) if d["missing"] else "dependency cycle %s" % d["cycle"]
actions.append({"issue": t["number"], "add_labels": ["agent:invalid"], "remove_labels": ["agent:ready"],
"comment": "Marked invalid: %s. Fix the Depends on field to return it to the queue." % why})
for t in sorted(tasks.values(), key=lambda x: x["number"]):
if t["state"] == "OPEN" and "agent:invalid" in t["labels"] and "agent:claimed" not in t["labels"]:
d = T.dep_problems(t, tasks)
if not d["missing"] and not d["cycle"]:
actions.append({"issue": t["number"], "add_labels": ["agent:ready"], "remove_labels": ["agent:invalid"],
"comment": "Dependencies are valid again; the task is back in the queue."})
return actions