Files
claude-config/bin/loop_claude/cycle/chunk_items.py
T

36 lines
2.2 KiB
Python

"""The todo.md items a chunk takes: those the worker names on its latest `Chunk items:` line, scored as todo.md scores them (docs/decisions.md → `routing: loop-claude reads the items a chunk takes`)."""
import re
from claude_code.entries import AssistantTurn, Entry, Text
from failure import Failure
from release_plan.todo_table import items
from claude_code.transcript import plain
from loop_claude.verdicts import TakenIn
# The label opening the line; prompts.NAME_THE_ITEMS asks the worker for it.
CHUNK_ITEMS = "Chunk items:"
# A line the worker opens with the label, after any list or quote marker; what follows names the items.
LINE = re.compile(rf"[\s>`-]*{re.escape(CHUNK_ITEMS)}(.*)")
# What the worker says of an item in parentheses, such as its L and E + I.
SAID_OF_AN_ITEM = re.compile(r"\([^)]*\)")
# One entry of the line, which commas or "and" separate: an item's ID, which may open with "item" or "#".
ITEM_ID = re.compile(r"\s*(?:item\s+)?#?(\d+)", re.IGNORECASE)
def taken_in(entries: list[Entry], todo: str | Failure) -> TakenIn:
"""The items named, with the highest L and E + I todo.md, as the cycle read it, scores among them. An item todo.md doesn't hold, or a todo.md that can't be read, counts at no level, so the route holds."""
ids = named(entries)
found = items(todo) if isinstance(todo, str) else todo
scored = [item for item in found if item.id in ids] if isinstance(found, list) else []
return TakenIn(ids, max((item.likelihood for item in scored), default=0), max((item.escape_and_impact for item in scored), default=0))
def named(entries: list[Entry]) -> tuple[int, ...]:
"""The IDs on the worker's latest Chunk items: line, each once, in its order; empty where it wrote none. Only the worker's own text counts: a loop-claude prompt quotes the line's example, and a tool call may write the label into a file."""
lines = [line for entry in entries if isinstance(entry, AssistantTurn) for part in entry.parts if isinstance(part, Text) for line in part.text.splitlines()]
for line in reversed(lines):
if found := LINE.match(plain(line)):
each = re.split(r",|\band\b", SAID_OF_AN_ITEM.sub("", found[1]))
return tuple(dict.fromkeys(int(item[1]) for named_item in each if (item := ITEM_ID.match(named_item))))
return ()