"""A worker's prompts: those the entry point driving the cycle words, and those that resume a worker with an answer to its question. An entry point is a tool that drives cycles, such as loop-claude. It words the prompts of its own workflow and names the files of its project. WorkerPrompts holds those, so the cycle doesn't import an entry point to word a prompt. """ import dataclasses import re import typing from model_routing.ladder import ProjectRung from model_routing.route import Route from claude_code.transcript import Typed # Opens typed's prompt, with the answer on the line after it, so typed_answer finds the answer at the prompt's start, whatever the question holds. TYPED_HERE = "The human answered your question at {driver}'s terminal:" # Finds TYPED_HERE whatever the driver, so a transcript reads the same to every tool that renders it. TYPED_OPENING = re.compile(re.escape(TYPED_HERE).replace(re.escape("{driver}"), r"[\w-]+") + "\n") def answers_recorded(destinations: str) -> str: """What a chunk owes each answer the human typed, before it is finished. destinations names where an answer may be recorded.""" return f"every answer the human typed in it that a later chunk could need is recorded in {destinations} at the one place it governs" def typed_answer(prompt: str) -> Typed | None: """Splits a prompt from WorkerPrompts.typed into its answer and the rest, so the judge reads the answer as the human's; None for any other prompt.""" opening = TYPED_OPENING.match(prompt) if not opening: return None answer, _, rest = prompt[opening.end():].partition("\n\n") return Typed(rest, answer) @dataclasses.dataclass(frozen=True) class WorkerPrompts: """What an entry point supplies to the prompts of its cycles.""" # The entry point's name as the worker, the judge and the human read it, such as `do-epic`. driver: str # The prompt that starts a worker, from its task, the premises, its route, the project's rung and the facts found before the cycle. fresh: typing.Callable[[str, str, Route, ProjectRung | None, str], str] # The prompt that resumes a worker whose previous run ended before the chunk was finished, from the evidence of the unfinished verdict, if there was one. resumed: typing.Callable[[str, Route, ProjectRung | None], str] # The sentences that depend on the worker's rung, each after a space, to follow the sentence a prompt ends on. rungs_held: typing.Callable[[Route, ProjectRung | None], str] # Where the judge may quote an answer from, named as the human and the worker see them. quote_sources: str # Where the worker may record an answer the human typed. answer_destinations: str # What the records the judge reads are, in the order it reads them. records_named: str def answered(self, question: str, answer: str, route: Route, project: ProjectRung | None, stepped: bool) -> str: """stepped is whether the answer's ruling stepped the worker up.""" return f"Your question: {question}\n\n{self.quote_sources} answers it:\n\n{answer}\n\nContinue the task.{self.stepped_to(route, project, stepped)}" def typed(self, question: str, answer: str, route: Route, project: ProjectRung | None, stepped: bool) -> str: """The prompt that resumes the worker with the human's answer to an interrupt. answer is one line, as the terminal reads it.""" return f"{TYPED_HERE.format(driver=self.driver)}\n{answer}\n\nYour question was: {question}\n\nContinue the task. The chunk is finished only once {answers_recorded(self.answer_destinations)}.{self.stepped_to(route, project, stepped)}" def stepped_to(self, route: Route, project: ProjectRung | None, stepped: bool) -> str: """The sentences naming the worker's new rung, as resumed has them, where the ruling that brought the answer stepped the worker up; empty otherwise.""" return f" Your request for a higher rung went through: {self.driver} runs you on {route.rung}. The escalation triggers that fired before this step are spent.{self.rungs_held(route, project)}" if stepped else "" def past_the_cap(self, question: str, answer: str, cap: int) -> str: """The question a quoted answer puts to the human once the cycle's quoted answers reach the cap.""" return f"{question}\n\n{self.quote_sources} says:\n{answer}\n\nThose files have answered {cap} questions this cycle, the most allowed, so this one comes to you."