Files
claude-config/bin/touch_predictions/beside.py
T

46 lines
2.2 KiB
Python

"""Which of a release's items may start chunks beside the next chunk, by what each is predicted to touch."""
import dataclasses
from failure import Failure
from plan.todo_table import Item, waiting
from touch_predictions.prediction import LABEL, overlaps, predicted, Touches
@dataclasses.dataclass(frozen=True)
class Beside:
"""The IDs of the items that may start chunks beside first's, in the plan's order, and why each other item may not."""
picked: list[int]
refused: list[str]
def chunks_beside(found: list[Item], first: Item) -> Beside | str | None:
"""The items that may start chunks while first's runs, with why each other item may not. Else why no item may, as a sentence's end; None where no item of first's release that doesn't wait holds a prediction, as in a project that doesn't write any.
An item may where it names first's release, doesn't wait, and overlaps neither first nor an item picked before it in the plan's order. The plan's order decides, not the largest set: a larger set could start a low-ranked item in place of one ranked above it. It cannot know which items first's chunk takes, so chunk-preflight's line tells its reader to leave those out."""
waits = waiting(found, set())
ready = [item for item in found if item.release == first.release and item.id not in waits]
if not any(LABEL in item.detail.splitlines() for item in ready):
return None
ours = predicted(first.detail)
if not isinstance(ours, dict):
return f"item {first.id}'s {unread(ours)}"
taken: dict[int, Touches] = {first.id: ours}
refused = []
for item in ready:
theirs = predicted(item.detail)
if item.id == first.id:
continue
if not isinstance(theirs, dict):
refused.append(f"item {item.id}, whose {unread(theirs)}")
continue
shared = next(((id_, places) for id_, touches in taken.items() if (places := overlaps(theirs, touches))), None)
if shared:
refused.append(f"item {item.id}, which overlaps item {shared[0]} at {', '.join(shared[1])}")
else:
taken[item.id] = theirs
return Beside([id_ for id_ in taken if id_ != first.id], refused)
def unread(prediction: Failure | None) -> str:
return f"detail has no `{LABEL}` list" if prediction is None else f"`{LABEL}` list cannot be read: {prediction.reason}"