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
21 changes: 16 additions & 5 deletions clerk/src/clerk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,20 @@ def _show_ref(runner: CommandRunner, root: Path, ref: str) -> bool:
return _git(runner, root, ["show-ref", "--verify", "--quiet", ref]).returncode == 0


def _returned_attempt_refs(runner: CommandRunner, root: Path, id_: str) -> tuple[bool, bool]:
if not (root / ".git").exists():
return False, False
main_root = _primary_repo_root(runner, root)
short = _returned_short_from_id(id_)
return (
_show_ref(runner, main_root, f"refs/heads/returned/{short}"),
_show_ref(runner, main_root, f"refs/remotes/origin/returned/{short}"),
)


def returned_attempt_banner(runner: CommandRunner, root: Path, short: str, id_: str, dispose_hint: str) -> None:
main_root = _primary_repo_root(runner, root)
local_present = _show_ref(runner, main_root, f"refs/heads/returned/{short}")
remote_present = _show_ref(runner, main_root, f"refs/remotes/origin/returned/{short}")
local_present, remote_present = _returned_attempt_refs(runner, root, id_)
if not local_present and not remote_present:
return
if local_present and remote_present:
Expand Down Expand Up @@ -474,7 +484,7 @@ def cmd_inbox_frontier(_backend: str, _root: Path, argv: Sequence[str], runner:
return 0


def cmd_backlog_next(backend: str, _root: Path, _argv: Sequence[str], runner: CommandRunner, _env: Mapping[str, str]) -> int:
def cmd_backlog_next(backend: str, root: Path, _argv: Sequence[str], runner: CommandRunner, _env: Mapping[str, str]) -> int:
if backend == "bd":
try:
pickable = BdWorkGraphAdapter(runner).backlog().pickable
Expand All @@ -486,7 +496,8 @@ def cmd_backlog_next(backend: str, _root: Path, _argv: Sequence[str], runner: Co
print(" (empty)")
return 0
for item in pickable:
print(f" {item.id} {item.title}")
marker = "returned" if any(_returned_attempt_refs(runner, root, item.id)) else "ready"
print(f" {item.id} {marker} {item.title}")
return 0
data = _json_from_result(runner.run(["gh", "issue", "list", "--label", "ready-for-agent", "--json", "number,title"]), "backlog next failed — gh issue list did not succeed")
if not isinstance(data, list):
Expand All @@ -498,7 +509,7 @@ def cmd_backlog_next(backend: str, _root: Path, _argv: Sequence[str], runner: Co
for item in data:
if not isinstance(item, dict):
backend_fail("backlog next failed — gh issue list did not return issue objects")
print(f" #{item.get('number')} {item.get('title', '')}")
print(f" #{item.get('number')} ready {item.get('title', '')}")
return 0


Expand Down
12 changes: 12 additions & 0 deletions clerk/src/clerk/work_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import json
import re
from dataclasses import dataclass
from typing import Any

Expand Down Expand Up @@ -89,6 +90,15 @@ def is_ready_promoted(value: dict[str, Any]) -> bool:
return "stage:ready" in (value.get("labels") or []) or str(value.get("close_reason") or "").startswith("promoted")


_ACCEPTANCE_HEADING = re.compile(r"^#{1,6}\s*acceptance criteria|^acceptance criteria:?\s*$", re.IGNORECASE | re.MULTILINE)


def has_acceptance_criteria(value: dict[str, Any]) -> bool:
if str(value.get("acceptance_criteria") or "").strip():
return True
return bool(_ACCEPTANCE_HEADING.search(f"{value.get('description') or ''}\n{value.get('design') or ''}"))


def is_active_inbox(value: dict[str, Any]) -> bool:
return value.get("status") in {"open", "in_progress"} and "stage:ready" not in (value.get("labels") or [])

Expand Down Expand Up @@ -203,6 +213,8 @@ def backlog(self) -> Backlog:
pickable: list[Work] = []
waiting: list[WaitingWork] = []
for item in unclaimed:
if not has_acceptance_criteria(item.raw):
continue
blockers = self.open_blockers(item)
children = self.open_children(item)
if blockers or children:
Expand Down
32 changes: 28 additions & 4 deletions tests/clerk/backlog-read.bats
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mk_ac_unit() { # $1 = title

# -------------------------------------------------------------- backlog next -

@test "backlog next (bd): includes a stage:ready+unblocked bead, excludes a non-ready open bead and a closed one; bd invoked with --readonly" {
@test "backlog next (bd): includes ready Work and excludes non-ready, closed, and missing-criteria Work; bd invoked with --readonly" {
repo=$(make_bd_repo next1)
tracedbin="$BATS_TEST_TMPDIR/next1-tracedbin"
make_traced_bd "$tracedbin"
Expand All @@ -137,36 +137,60 @@ mk_ac_unit() { # $1 = title
cd "$repo"
ready_id=$(mk_ac_unit "ready and unblocked")
bd update "$ready_id" --add-label stage:ready >/dev/null
missing_id=$(bd create "ready label but no criteria" --silent)
bd update "$missing_id" --add-label stage:ready >/dev/null
notready_id=$(bd create "still in the inbox" --silent)
closed_id=$(bd create "long done" --silent)
bd close "$closed_id" --reason wontfix >/dev/null

run "$CLERK" backlog next
[ "$status" -eq 0 ]
[[ "$output" == "Backlog (ready) — 1 item(s):
$ready_id ready and unblocked" ]]
$ready_id ready ready and unblocked" ]]
[[ "$output" != *"$missing_id"* ]]
[[ "$output" != *"$notready_id"* ]]
[[ "$output" != *"$closed_id"* ]]
grep -q -- '--readonly' "$BD_TRACE_LOG"
}

@test "backlog next (bd): marks an otherwise-pickable returned attempt without changing the pool" {
repo=$(make_bd_repo next_returned)
add_origin "$repo"
cd "$repo"
normal=$(mk_ac_unit "normal Work")
returned=$(mk_ac_unit "returned Work")
bd update "$normal" --add-label stage:ready >/dev/null
bd update "$returned" --add-label stage:ready >/dev/null
short="${returned#*-}"
mk_returned_attempt "$repo" "$short" "returned backlog work"

run "$CLERK" backlog next
[ "$status" -eq 0 ]
[[ "$output" == "Backlog (ready) — 2 item(s):"$'\n'* ]]
[[ "$output" == *" $normal ready normal Work"* ]]
[[ "$output" == *" $returned returned returned Work"* ]]
}

@test "backlog next and waiting split pickable ready work from blocked-ready graph work" {
repo=$(make_bd_repo next_waiting)
cd "$repo"
pickable=$(mk_ac_unit "pickable")
blocked=$(mk_ac_unit "blocked ready")
claimed=$(mk_ac_unit "claimed ready")
blocker=$(bd create "blocker" --silent)
parent=$(bd create "ready parent" --acceptance "parent ac" --silent)
child=$(bd create "open child" --parent "$parent" --silent)
bd update "$pickable" --add-label stage:ready >/dev/null
bd update "$blocked" --add-label stage:ready >/dev/null
bd update "$parent" --add-label stage:ready >/dev/null
bd update "$claimed" --add-label stage:ready --claim >/dev/null
bd dep add "$blocked" "$blocker" >/dev/null

run "$CLERK" backlog next
[ "$status" -eq 0 ]
[[ "$output" == *"$pickable pickable"* ]]
[[ "$output" == *"$pickable ready pickable"* ]]
! grep -F -q " $blocked " <<<"$output"
! grep -F -q " $claimed " <<<"$output"
! grep -F -q " $parent " <<<"$output"

run "$CLERK" backlog waiting
Expand Down Expand Up @@ -199,7 +223,7 @@ mk_ac_unit() { # $1 = title
run "$CLERK" backlog next
[ "$status" -eq 0 ]
[ "$output" = "Backlog (ready) — 1 item(s):
#9 do the thing" ]
#9 ready do the thing" ]
grep -F -q -- 'issue\x1flist\x1f--label\x1fready-for-agent' "$FAKE_GH_LOG"
}

Expand Down
4 changes: 2 additions & 2 deletions tests/clerk/inbox.bats
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ JSON
run "$CLERK" backlog next
[ "$status" -eq 0 ]
[ "$output" = "Backlog (ready) — 2 item(s):
#57 ready GitHub delivery
#47 another ready GitHub delivery" ]
#57 ready ready GitHub delivery
#47 ready another ready GitHub delivery" ]
[[ "$output" != *"bd-only raw capture"* ]]
grep -F -q -- 'issue\x1flist\x1f--label\x1fready-for-agent' "$FAKE_GH_LOG"
}
Expand Down
12 changes: 7 additions & 5 deletions tests/clerk_python/test_read_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,27 @@ def test_inbox_graph_usage_not_found_and_backend_failures(self):

def test_backlog_next_filters_ready_items_without_serial_show_fanout(self):
rows = [
{"id": "pick", "title": "Pick", "status": "open", "labels": ["stage:ready"]},
{"id": "pick", "title": "Pick", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- pick"},
{
"id": "blocked",
"title": "Blocked",
"status": "open",
"labels": ["stage:ready"],
"acceptance_criteria": "- blocked",
"dependencies": [{"type": "blocks", "depends_on_id": "blocker"}],
},
{"id": "blocker", "title": "Blocker", "status": "open"},
{"id": "parent", "title": "Parent", "status": "open", "labels": ["stage:ready"]},
{"id": "parent", "title": "Parent", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- parent"},
{"id": "child", "title": "Child", "status": "open", "parent": "parent"},
{"id": "claimed", "title": "Claimed", "status": "open", "labels": ["stage:ready"], "assignee": "me"},
{"id": "claimed", "title": "Claimed", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- claimed", "assignee": "me"},
{"id": "missing", "title": "Missing", "status": "open", "labels": ["stage:ready"]},
]

code, out, _, calls = self.invoke(
("backlog", "next"), "bd", [], [lambda args: ok(args, json.dumps(rows))]
)
self.assertEqual(code, 0)
self.assertEqual(out, "Backlog (ready) — 1 item(s):\n pick Pick\n")
self.assertEqual(out, "Backlog (ready) — 1 item(s):\n pick ready Pick\n")
self.assertEqual(calls, [["bd", "list", "--all", "--readonly", "--json", "--limit", "0"]])

def test_backlog_next_gh_lists_ready_for_agent_issues(self):
Expand All @@ -227,7 +229,7 @@ def test_backlog_next_gh_lists_ready_for_agent_issues(self):
("backlog", "next"), "gh", [], [lambda args: ok(args, json.dumps(payload))]
)
self.assertEqual(code, 0)
self.assertEqual(out, "Backlog (ready) — 1 item(s):\n #9 do the thing\n")
self.assertEqual(out, "Backlog (ready) — 1 item(s):\n #9 ready do the thing\n")
self.assertEqual(calls, [["gh", "issue", "list", "--label", "ready-for-agent", "--json", "number,title"]])

def test_show_not_found_is_usage_error(self):
Expand Down
18 changes: 15 additions & 3 deletions tests/clerk_python/test_work_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ def run(self, args, *, cwd=None, env=None):
class BdWorkGraphAdapterTests(unittest.TestCase):
def test_backlog_distinguishes_ready_pickable_and_waiting_in_one_snapshot(self):
payload = [
{"id": "pick", "title": "Pick", "status": "open", "labels": ["stage:ready"]},
{"id": "pick", "title": "Pick", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- pick"},
{
"id": "blocked",
"title": "Blocked",
"status": "open",
"labels": ["stage:ready"],
"acceptance_criteria": "- blocked",
"dependencies": [{"type": "blocks", "depends_on_id": "blocker"}],
},
{"id": "blocker", "title": "Blocker", "status": "open"},
{"id": "parent", "title": "Parent", "status": "open", "labels": ["stage:ready"]},
{"id": "parent", "title": "Parent", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- parent"},
{"id": "child", "title": "Child", "status": "open", "parent": "parent"},
{"id": "claimed", "title": "Claimed", "status": "open", "labels": ["stage:ready"], "assignee": "agent"},
{"id": "claimed", "title": "Claimed", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- claimed", "assignee": "agent"},
{"id": "inbox", "title": "Inbox", "status": "open"},
]
runner = FakeRunner(payload)
Expand Down Expand Up @@ -78,6 +79,7 @@ def test_closed_children_and_blockers_stop_withholding_a_ready_parent(self):
"title": "Parent",
"status": "open",
"labels": ["stage:ready"],
"acceptance_criteria": "- parent",
"dependencies": [{"type": "blocks", "depends_on_id": "blocker"}],
},
{"id": "blocker", "title": "Blocker", "status": "closed"},
Expand All @@ -89,6 +91,16 @@ def test_closed_children_and_blockers_stop_withholding_a_ready_parent(self):
self.assertEqual([item.id for item in backlog.pickable], ["parent"])
self.assertEqual(backlog.waiting, ())

def test_backlog_excludes_ready_labels_without_acceptance_criteria(self):
payload = [
{"id": "missing", "title": "Missing", "status": "open", "labels": ["stage:ready"]},
{"id": "valid", "title": "Valid", "status": "open", "labels": ["stage:ready"], "acceptance_criteria": "- valid"},
]

backlog = BdWorkGraphAdapter(FakeRunner(payload)).backlog()

self.assertEqual([item.id for item in backlog.pickable], ["valid"])


if __name__ == "__main__":
unittest.main()
Loading