"""The plan items a chunk takes: those the worker names on its latest `Chunk items:` line, scored as the plan 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 loop_claude.quotes import plain from plan_store.plans import Plan 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 `-` or `>` marker or a backtick; 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"\([^)]*\)") # An entry of the line, the text between commas or "and": an item's ID, after an optional "item" or "#". ITEM_ID = re.compile(r"\s*(?:item\s+)?#?(\d+)", re.IGNORECASE) def taken_in(entries: list[Entry], plan: Plan | Failure) -> TakenIn: """The items named, with the highest L and the highest E + I that the plan, as read when the cycle started, gives any of them. An item the plan doesn't hold, or a plan that can't be read, adds no level, so the route holds.""" ids = named(entries) scored = [] if isinstance(plan, Failure) else [item for item in plan.items if item.id in ids] return TakenIn(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 ()