65 lines
5.1 KiB
Python
65 lines
5.1 KiB
Python
from model_routing.ladder import climbed, highest, LADDER, Rung
|
|
from model_routing.route import ESCALATION_MARKER, Route
|
|
from loop_claude.routing import escalation_rungs
|
|
from loop_claude.project.records import HUMAN_RULE_NAMES, RECORD_FILES
|
|
from markdown_text import quoted
|
|
|
|
def either(names: tuple[str, ...]) -> str:
|
|
"""The names as `a, b or c`."""
|
|
return f"{', '.join(names[:-1])} or {names[-1]}"
|
|
|
|
|
|
# Where the judge may quote an answer from, named as the human and the worker see them; of README.md the judge sees only the premises.
|
|
QUOTE_SOURCES = either(("README.md's Goals and Audience", *RECORD_FILES, *HUMAN_RULE_NAMES))
|
|
# Where the worker may write an answer the human typed, so it counts as recorded.
|
|
ANSWER_DESTINATIONS = "README.md's Goals or Audience, " + ", ".join(RECORD_FILES) + ", todo.md or a comment"
|
|
ANSWERS_RECORDED = f"every answer the human typed in it that a later chunk could need is recorded in {ANSWER_DESTINATIONS} at the one place it governs"
|
|
|
|
|
|
def fresh(task: str, premises: str, route: Route, anchor: Rung | None) -> str:
|
|
escalated = escalation(route, anchor).lstrip()
|
|
routed = f"{routed_by(route, anchor)}\n\n" if route.item else (f"{escalated}\n\n" if escalated else "")
|
|
return f"{task}\n\nContinue any open draft PR or worktree for the chunk before creating a new one.\n\n{routed}The chunk's todo.md items, or else the task above, set its scope. The goals below choose between ways to deliver that scope, and never widen it.\n\nFrom README.md:\n{premises}"
|
|
|
|
|
|
def routed_by(route: Route, anchor: Rung | None) -> str:
|
|
return f"loop-claude runs you on {route.rung}, routed by item {route.item}'s R + C of {route.risk_and_complexity}. Name each todo.md item this chunk takes, with its R + C, whether or not it is item {route.item}.{off_the_projects_rung(route, anchor)}{escalation(route, anchor)}"
|
|
|
|
|
|
def off_the_projects_rung(route: Route, anchor: Rung | None) -> str:
|
|
"""Which subagents to launch on the project's rung, anchor, and below it which on the --plan rung; empty while the worker runs on anchor, as every chunk no item routed does until it steps up."""
|
|
if anchor is None or route.rung == anchor:
|
|
return ""
|
|
# Below the project's rung, the reviewers stay on it to hold quality; above it, the seats score every chunk on one rung, and the security pass spends no more than the project chose.
|
|
below = highest(route.rung, anchor) == anchor
|
|
launched = "each reviewer, each comprehension seat and the security pass's subagent" if below else "each comprehension seat and the security pass's subagent"
|
|
planning = f" Launch each subagent that files or scores todo.md items on the rung `~/.claude/bin/subagent-rung --plan {anchor.model} <R+C>` prints." if below else ""
|
|
return f" Your rung is {'below' if below else 'above'} the project's, {anchor}: launch {launched} with the Agent tool set to {anchor.agent_args()}. Run a full scan only from such a subagent, since the scan's agents run on the model that starts them.{planning}"
|
|
|
|
|
|
def escalation(route: Route, anchor: Rung | None) -> str:
|
|
"""The rungs an escalation trigger opens to the worker, from one above its own up to two above the rung its chunk started on, and how to mark an escalated subagent's prompt; empty off the ladder or on its top rung."""
|
|
if anchor is None or route.rung not in LADDER[:-1]:
|
|
return ""
|
|
marked = f" Open the subagent's prompt with `{ESCALATION_MARKER} <n>:`, n the trigger's number."
|
|
rungs = [f"`{rung.agent_args()}`" for rung in escalation_rungs(anchor, route)]
|
|
if not rungs:
|
|
return f" An escalation trigger in `~/.claude/CLAUDE.md` lets you hand a task to a subagent with the Agent tool set to `{climbed(route.rung, 1).agent_args()}`, one rung above yours, only once the human agrees.{marked}"
|
|
further = f", and on each further trigger one rung higher, up to {rungs[-1]}" if len(rungs) > 1 else ""
|
|
return f" When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the task to a subagent with the Agent tool set to {rungs[0]}, one rung above yours{further}; above that, ask the human first.{marked}"
|
|
|
|
|
|
def resumed(unfinished_evidence: str, route: Route, anchor: Rung | None) -> str:
|
|
"""Names the project's rung again, since a step-up between cycles may have taken the worker above it."""
|
|
ruled = f" A judge ruled the chunk unfinished, quoting this passage of your transcript as the work remaining:\n\n{quoted(unfinished_evidence)}\n\n" if unfinished_evidence else " "
|
|
return f"Continue the task; the previous run ended before the chunk was finished.{ruled}The chunk is finished once it is merged, or nothing remains that you could do on your own. Even then, it is finished only once {ANSWERS_RECORDED}.{off_the_projects_rung(route, anchor)}{escalation(route, anchor)}"
|
|
|
|
|
|
def answered(question: str, answer: str) -> str:
|
|
return f"Your question: {question}\n\n{QUOTE_SOURCES} answers it:\n\n{answer}\n\nContinue the task."
|
|
|
|
|
|
def past_the_cap(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{QUOTE_SOURCES} says:\n{answer}\n\nThose files have answered {cap} questions this cycle, the most allowed, so this one comes to you."
|