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
3 changes: 2 additions & 1 deletion agents/conductors/batch/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ The reap keeps what the worker left before deleting its marker: the `.done`
lines go to `outcomes` (`<url>`; a `merged <url>` line, which no shift
worker writes since 2026-11-19, is still recorded as an in-turn merge), and `<task>.proof.md` moves to `<slot>/proofs/`. The first tick after
`review-at:` runs `finish_shift` once: `_digest.build` writes the `## Digest`
(`PyAutoMind/batches/AGENTS.md` format — brief work, backlog picks each with
(`PyAutoMind/batches/AGENTS.md` format — first a numbered review table grouped
by the human's next action (#718), then brief work, backlog picks each with
its `why:`, one proof packet per started task or a stub that says none was
written, every counter) to `<slot>/digest.md`, appends it to the record through
the one Mind writer, writes `digest.html` only under `packet: yes`, then
Expand Down
225 changes: 223 additions & 2 deletions agents/conductors/batch/_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,208 @@ def held_leases(slot: Path) -> dict[str, dict]:
return out


# --- the review table ----------------------------------------------------------

REVIEW_HEADING = "### Review table"
#: The table's groups, by the human's next action, most urgent first. A task
#: sits in exactly one: the most urgent of its PRs' groups.
G_MERGE, G_ANSWER, G_FIX, G_BLOCKED, G_RUNNING, G_DONE, G_PROPOSED = range(7)
REVIEW_GROUPS = ("Merge: your /prm", "Answer: question on the issue",
"Fix: CI failed or crashed", "Blocked or not started", "Still running",
"Done", "Proposed (shadow)")
#: A `plain_outcome` word's group; the first prefix that matches wins. An
#: unknown word goes to Merge: it fails toward the human's attention. The
#: digest is written at `review-at:` and the late sweep never closes a PR
#: out ("what arrives late waits for /prm"), so a PR whose CI is still
#: running, or held on an unknown workspace sweep, is the human's /prm too.
#: Only a close-out at work and a worker still out are "still running".
_GROUP_OF = (
("Merged with NO merge slot", G_MERGE),
("Merged", G_DONE), ("Already merged", G_DONE), ("Verdict posted", G_DONE),
("Finished", G_BLOCKED),
("CI failed", G_FIX), ("Abandoned", G_FIX), ("Crashed", G_FIX), ("Killed", G_FIX),
("Needs you — question", G_ANSWER), ("Needs you — blocked", G_ANSWER),
("Needs you — not merged", G_MERGE), ("Waiting — merges after", G_MERGE),
("CI green", G_MERGE), ("No CI checks", G_MERGE), ("CI unknown", G_MERGE),
("CI running", G_MERGE), ("Waiting — workspace sweep", G_MERGE),
("Still running", G_RUNNING),
("Obsolete", G_BLOCKED), ("Not started", G_BLOCKED),
("Proposed", G_PROPOSED),
)
#: The CI word that ends an UPDATED or CURRENT state (`… CI on new head FAILED`).
_TRAILING_CI_RE = re.compile(r"\bCI (?:on new head )?([A-Z][A-Z-]*)\s*$")
#: The CI words that send a PR to Fix.
_CI_RED = ("FAILED", "RED")
#: The prefixes `task_state` puts on a task the human dropped by `/drop`.
_DROPPED = ("dropped, still running — ", "dropped by /drop — ")
#: A note longer than this is cut, with an ellipsis.
NOTE_MAX = 90
_WORK_REF_RE = re.compile(r"^https://github\.com/[^/\s]+/([^/\s]+)/(?:pull|issues)/(\d+)")
_URL_PART_RE = re.compile(r"; (?=https?://)")


def work_ref(url: str) -> str:
"""`repo#n` for a GitHub PR or issue URL; the URL itself otherwise."""
m = _WORK_REF_RE.match(url or "")
return f"{m.group(1)}#{m.group(2)}" if m else (url or "?")


def _cell(text: str) -> str:
"""Text safe in a Markdown table cell: one line, `<` and `>` as entities
(GitHub would read them as HTML), pipes escaped once."""
text = " ".join(str(text or "").split()).replace("<", "&lt;").replace(">", "&gt;")
return re.sub(r"(?<!\\)\|", r"\\|", text)


def _group_of(word: str) -> int:
return next((g for p, g in _GROUP_OF if word.startswith(p)), G_MERGE)


def _trailing_ci(what: str) -> str:
"""The CI word an UPDATED or CURRENT state ends on, or ""."""
if not what.startswith((UPDATED, CURRENT)):
return ""
m = _TRAILING_CI_RE.search(what)
return m.group(1) if m else "UNKNOWN"


def _part_group(what: str) -> int:
"""One part of a task's state as a review group. A branch updated from
main (or already up to date) is judged by its CI word: red is Fix, any
other is the human's /prm."""
ci = _trailing_ci(what)
if ci:
return G_FIX if ci in _CI_RED else G_MERGE
return _group_of(plain_outcome(what))


def _state_parts(state: str) -> list[tuple[str, str]]:
"""A `task_state` as (url, what) per URL it names; ("", state) when it
names none."""
parts = []
for part in _URL_PART_RE.split(state):
if part.startswith("http") and " — " in part:
url, what = part.split(" — ", 1)
parts.append((url, what))
else:
parts.append(("", part))
return parts


def _part_note(url: str, what: str) -> str:
"""The short reason one part of a task's state gives, or ""."""
if what.startswith("awaiting /prm — close-out refused: "):
why = what[len("awaiting /prm — close-out refused: "):]
return refusal_class(why).split(" — ", 1)[0]
if what.startswith("PARKED-ON-ISSUE"):
return f"question on {work_ref(url)}" if url else "question on the issue"
if what.startswith("waits behind"):
return "" # the Work cell says "after repo#m"
for head in ("not started — skipped: ", "not started — refused: ",
NOT_REDISPATCHED + ": ", "obsolete (premise check): "):
if what.startswith(head):
return what[len(head):].split(" — ", 1)[0]
if what.startswith(IN_TURN_NO_SLOT):
return "merged with NO merge slot"
if what.startswith(("parked", "blocked on a person")):
return "blocked on a person"
ci = _trailing_ci(what)
if ci:
return ("branch updated" if what.startswith(UPDATED) else "branch up to date") \
+ f", CI {ci}"
if what.startswith("RUNNING"):
return "CI still running"
if what.startswith("waiting: workspace sweep unknown"):
return "workspace sweep unknown"
if what.startswith("finished, no PR or issue named"):
return "finished, no PR or issue named"
return plain_outcome(what)


def merge_ordered(prs: list[str], waits: dict) -> list[str]:
"""A task's PRs in merge order: as its `.done` named them, except that a
PR waiting behind another of them comes after it."""
out: list[str] = []

def place(u, seen=()):
if u in out or u in seen:
return
for b in (waits.get(u) or {}).get("behind") or []:
if b in prs:
place(b, (*seen, u))
out.append(u)
for u in prs:
place(u)
return out


def review_row(task: str, counters: dict, state: str, tier: str,
links: MindLinks | None = None) -> dict:
"""One task as a review-table row: its group, the Task, Tier, Work and
Note cells, and how many open PRs it leaves for the human."""
pick = counters.get("picks", {}).get(task) or {}
out = (counters.get("outcomes") or {}).get(task) or {}
dropped = next((p for p in _DROPPED if state.startswith(p)), "")
parts = _state_parts(state[len(dropped):])
group = min((_part_group(what) for _, what in parts), default=G_MERGE)
# A dropped task is Blocked, unless a part of it still needs the human:
# a merge with no slot, or an open PR to merge or fix.
if dropped and group not in (G_MERGE, G_FIX):
group = G_BLOCKED
waits = counters.get("merge_waits") or {}
prs = merge_ordered(list(out.get("prs") or []), waits)
work = []
for url in prs + list(out.get("issues") or []):
ref = f"[{_cell(work_ref(url))}]({url})"
behind = (waits.get(url) or {}).get("behind") or []
work.append(ref + (" after " + ", ".join(map(work_ref, behind)) if behind else ""))
notes = (["dropped by /drop"] if dropped else []) + [
n for url, what in parts for n in [_part_note(url, what)] if n]
note = "; ".join(dict.fromkeys(notes))
if len(note) > NOTE_MAX:
note = note[:NOTE_MAX - 1].rstrip() + "…"
title = _cell(pick.get("title") or task)
mind_url = links.prompt_url(pick.get("prompt") or "") if links else ""
done = set(out.get("merged") or []) | set(out.get("verdicts") or []) \
| settled_prs(counters)
return {"task": task, "group": group,
"cells": [f"[{title}]({mind_url})" if mind_url else title,
_cell(tier) if tier and tier != "?" else "—",
"<br>".join(work) or "—", _cell(note) or "—"],
"open_prs": sum(1 for u in prs if "/pull/" in u and u not in done)}


def _count_line(rows: list[dict]) -> str:
n = [sum(1 for r in rows if r["group"] == g) for g in range(len(REVIEW_GROUPS))]
prs = sum(r["open_prs"] for r in rows if r["group"] == G_MERGE)
words = (f"{n[G_MERGE]} to /prm" + (f" ({prs} PR{'s' if prs != 1 else ''})" if prs else ""),
f"{n[G_ANSWER]} to answer", f"{n[G_FIX]} to fix",
f"{n[G_BLOCKED]} blocked or not started", f"{n[G_RUNNING]} still running",
f"{n[G_DONE]} done", f"{n[G_PROPOSED]} proposed")
return " · ".join(w for g, w in enumerate(words) if n[g])


def review_table_lines(rows: list[dict]) -> list[str]:
"""The `### Review table`: a count line, then one table of every named
task and backlog pick, grouped by the human's next action (a bold row
per group, empty groups left out) and numbered 1…N straight through, so
a reply can say "prm 1–4, 6"."""
out = [REVIEW_HEADING]
if not rows:
return out + ["Nothing to review — no tasks this shift.", ""]
out += [_count_line(rows), "", "| # | Task | Tier | Work | Note |", "|---|---|---|---|---|"]
n = 0
for g, name in enumerate(REVIEW_GROUPS):
group = [r for r in rows if r["group"] == g]
if not group:
continue
out.append(f"| | **{name}** | | | |")
for r in group:
n += 1
out.append(f"| {n} | " + " | ".join(r["cells"]) + " |")
return out + [""]


def _line(task: str, counters: dict, tier: str, health, links: MindLinks | None = None,
why: str = "", lease: dict | None = None) -> str:
state, tiered = task_state(task, counters, health, lease=lease)
Expand All @@ -764,12 +966,23 @@ def _packet(task: str, slot: Path, counters: dict) -> str:
"- revert: see the PR")


def _memo(health):
"""`health`, read once per URL."""
seen: dict = {}

def once(url):
if url not in seen:
seen[url] = health(url)
return seen[url]
return once


def build(brief: dict, slot: Path, counters: dict, *, tiers: dict | None = None,
health=None, issue: str = "", mind=None) -> str:
"""The `## Digest` block for one shift, as Markdown. `mind` is the
watcher's Mind checkout, read locally for the prompt links."""
tiers = tiers or {}
health = health or pr_health
health = _memo(health or pr_health) # the table and the entries read it once
try: # the slot file is the merge evidence; fresh at build time
counters = {**counters, "slot_holders": _merge_slots.holders(slot)}
except (_merge_slots.MergeSlotError, OSError):
Expand All @@ -787,10 +1000,18 @@ def build(brief: dict, slot: Path, counters: dict, *, tiers: dict | None = None,
inbox = [x for x in [_questions.inbox_line(counters, Path(slot).name, mind)]
+ _questions.unfiled_lines(counters) if x]
lines += inbox + [""] if inbox else []
lines += ["### Brief work"]
links = MindLinks(mind, issue)
# A worker still out at review-at keeps its lease.
held = {t: l for t, l in held_leases(slot).items() if l.get("state") != "parked"}
# The review table: every named task and backlog pick (a settled
# carry-over is in neither, so it is left out here too).
rows = []
for t in named + backlog:
tier = tiers.get(t, picks.get(t, {}).get("tier", "?"))
state, _ = task_state(t, counters, health, lease=held.get(t))
rows.append(review_row(t, counters, state, tier, links))
lines += review_table_lines(rows)
lines += ["### Brief work"]
lines += [_line(t, counters, tiers.get(t, picks.get(t, {}).get("tier", "?")), health, links,
lease=held.get(t)) for t in named] or [NO_PICKS]
lines += ["", "### Backlog picks"]
Expand Down
3 changes: 2 additions & 1 deletion skills/batch/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ reached; `batch.md` points here from each one.
## Under a brief, the digest is the review surface

**Under a brief, the digest is the review surface** (2026-11-03). The watcher
appends a `## Digest` to the record — brief work, then backlog picks with a
appends a `## Digest` to the record — a review table grouped by what the human
does next (merge, answer, fix, blocked, running, done), then brief work, then backlog picks with a
one-line reason each, one proof packet per task, then the shift counters — and
posts it once as a comment on the shift's `issue:`. The record plus that comment
is what the human reads; the HTML packet is written only when the brief says
Expand Down
Loading
Loading